registration and login page is working properly but mine like button is not working .. I don't know why... Can somebody help me to solve this issue … it will be great help please help Thank you!

  1. views.py`

     from django.shortcuts import render, get_object_or_404
     from datasecurity.models import Post
     from django.urls import reverse
     from django.http import HttpResponseRedirect
     from django.contrib.auth.decorators import login required
    
    
     # Create your views here.
     def datasecurity(request):
          allPosts= Post.objects.all()
          context={'allPosts': allPosts}
           return render(request, 'datasecurity/data.html',context=context)
    
     def blogHome(request, slug):
          post=Post.objects.filter(slug=slug).first()
          context={"post":post}
          return render(request, "datasecurity/blogHome.html", context)
    
     @login_required
     def likes(request, pk):
          post=get_object_or_404(Post, id=request.POST.get('post_id'))
          post.likes.add(request.user)
          return HttpResponseRedirect(reverse('datasecurity:blogHome', args=str(pk)))
    
  2. urls.py

    from django.conf.urls import url
    
    from . import views
    
    app_name = 'datasecurity'
    
    urlpatterns = [
            url(r'^$', views.datasecurity, name="datasecurity"),
            url(r'^datasecurity/(?P<slug>[^/]+)', views.blogHome, name='blogHome'),
            url(r'^likes/<int:pk>', views.likes, name = "likes"),
    
    ]
    
  3. data.html

     {% extends 'careforallapp/navbar.html' %}
     {% block body_block %}
      {% load static %}
    

    Welcome to Data Security

        {% for post in allPosts  %}
    
         <div class="line-dec"></div>
         <span
           >This is a Bootstrap v4.2.1 CSS Template for you. Edit and use
           this layout for your site. Updated on 21 May 2019 for repeated main menu HTML code.</span
         >
       </div>
       <div class="left-image-post">
         <div class="row">
           <div class="col-md-6">
             <div class="left-image">
               {% if post.img %}
                 <img src="{{ post.img.url }}" alt="" />
               {% endif %}
             </div>
           </div>
           <div class="col-md-6">
             <div class="right-text">
               <h4>{{post.title}}</h4>
               <h6>Article  by {{post.author}}</h6>
               <h2>{{post.datetime}}</h2>
    
               <p>
                 {{post.content|safe | truncatechars:280}}
               </p>
    
               <from action = "{% url 'datasecurity:likes' pk=post.pk %}" method = "POST">
                 {% csrf_token %}
                 <button type="submit" name="post_id" value = "{{ post_id }}" class="btn"> Like 
               </button>
               </form>
    
               <div class="white-button">
                 <a href="{% url 'datasecurity:blogHome' slug=post.slug %}">Read More</a>
               </div><br>
             </div>
                {% endfor %}
    
  4. error msg <from action = "{% url 'datasecurity:likes' pk=post.pk %}" method = "POST">

  • Reverse for 'likes' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['datasecurity/likes/int:pk']

this msg was highlighted when I debug the the code. So can someone tell me please what i missed in my code... Thank You!

1

There are 1 best solutions below

6
Siva Sankar On BEST ANSWER

You are not taking the pk argument from view. Change your view to:

@login_required
 def likes(request, pk):
      post=get_object_or_404(Post, id=pk)

and the url to:

url(r'^likes/(?P<pk>\d+)/', views.likes, name = "likes"),

notice the used regular expression in the url.