Error caused by total_likes in Function Django

Submitted 2 years ago
Ticket #4
Views 119
Language 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 2 years 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 2 years ago

no it is a function to the model

- ahmed.hisham87 2 years 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()

- arun 2 years ago

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

- ahmed.hisham87 2 years ago

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

- arun 2 years 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 2 years 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

- arun 2 years 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 2 years ago

remove self

- arun 2 years 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 2 years 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.

- arun 2 years ago

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

- ahmed.hisham87 2 years ago

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

- arun 2 years ago

We hope issue has been resolved.

Submitted 2 years ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message