Error caused by total_likes in Function Django

Submitted 3 years, 11 months ago
Ticket #4
Views 228
Language/Framework Django
Priority Low
Status Closed

Hi again friends,

The total_likes have been hashtagged/commented in the LikeView.

The count is working perfectly in the admin and with every like it is added to the count because in the Post Detail View it is recognized, it is only not working in the LikeView.

I want to know why the hashtagged/commented part is wrong and how to resolve it.

class PostDetailView(DetailView):
    model = Post
    template_name = "post_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super(PostDetailView, self).get_context_data()
        stuff = get_object_or_404(Post, id=self.kwargs['pk'])
        total_likes = stuff.total_likes()
        liked = False
        if stuff.likes.filter(id=self.request.user.id).exists():
            liked = True
        context["total_likes"] = total_likes
        context["liked"] = liked
        return context

def LikeView(request):
    post = get_object_or_404(Post, id=request.POST.get('id'))
    # stuff = get_object_or_404(Post, id=self.kwargs['pk'])
    # total_likes = stuff.total_likes()
    liked = False
    if post.likes.filter(id=request.user.id).exists():
        post.likes.remove(request.user)
        liked = False
    else:
        post.likes.add(request.user)
        liked = True

    context = {
        # 'total_likes': total_likes,
        'liked': liked,
        'post': post
    }

    if request.is_ajax:
        html = render_to_string('like_section.html', context, request=request)
        return JsonResponse({'form': html})

Submitted on May 20, 20
add a comment

2 Answers

Verified

Is total_likes() is a method in model? If yes, can you share the details?

Submitted 3 years, 11 months ago

class Post(models.Model): designer = models.ForeignKey(User, on_delete=models.CASCADE) design = models.ImageField( blank=False, null=True, upload_to=upload_design_to) title = models.CharField(max_length=100) date_posted = models.DateTimeField(default=timezone.now) likes = models.ManyToManyField( User, related_name='liked')

def total_likes(self):
    return self.likes.count()
- ahmed.hisham87 3 years, 11 months ago

no it is a function to the model

- ahmed.hisham87 3 years, 11 months ago

can you add print statement here on this function in your models & see def total_likes(self): print(self.likes.count()) return self.likes.count()

- Vengat 3 years, 11 months ago

it is printing the number of counts correctly but I have to refresh the page

- ahmed.hisham87 3 years, 11 months ago

so you mean count is getting into the view but it not reflecting using ajax right?

- Vengat 3 years, 11 months ago

yes and the reason for this the LikeView function I have hashtagged the count as it is causing the like button not to work at all. Please check the LikeView function in the views and the hashtagged/commented codes

- ahmed.hisham87 3 years, 11 months ago

Uncomment the #likes in views .. "stuff = get_object_or_404(Post, id=self.kwargs['pk'])" change print(stuff) and see whether its returning data or not

- Vengat 3 years, 11 months ago

nothing happens and i get this in the traceback stuff = get_object_or_404(Post, id=self.kwargs['pk']) NameError: name 'self' is not defined

- ahmed.hisham87 3 years, 11 months ago

remove self

- Vengat 3 years, 11 months ago

I got this error "NameError: name 'kwargs' is not defined" so I removed kwargs so I am getting now this error stuff = get_object_or_404(Post, id=['pk']): TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

- ahmed.hisham87 3 years, 11 months ago

thats true because you are not having args and *kwargs argument in your view right?.. So here is my recommendation, in your ajax call, make sure , you are sending the PK value or not. If yes then your URL is capturing that PK argument or not , and finally get the PK into view as an argument. If you are not clear then we will create some video and update here after sometime.

- Vengat 3 years, 11 months ago

it is capturing pk, i just need to fix these 2 lines of codes to get the total_likes()

- ahmed.hisham87 3 years, 11 months ago

can you add args , *kwargs in your view like this def Likeview(request, args, *kwargs)" after that print(args,kwargs)

- Vengat 3 years, 11 months ago

We hope issue has been resolved.

Submitted 3 years, 11 months ago


Latest Blogs