After user click on like button page should not refresh(redirect) and update count of likes

281 views
Skip to first unread message

Salima Begum

unread,
May 4, 2021, 11:30:59 PM5/4/21
to django...@googlegroups.com
When user click to like a post the web page should not refresh(redirect) and count of the likes should be updated.
 How can I achieve this please help me.

Here is my block of code
```
<a style="{% if request.session.email %}pointer-events: auto;{% else %}pointer-events: none;{% endif %}"
                                  href="/post_comment_like/{{j.id}}">
                                   <i class="fa fa-thumbs-o-up" aria-hidden="true"
                                      style="color: gray;"></i></a>
```
views.py
```
def post_comment_like(request, id):
    if (request.session.get('email') is None) or (request.session.get('email') == ""):
        return HttpResponseRedirect("/home")

    email = request.session.get('email')
    qury = Comment.objects.get(id=id)
    obj_id = qury.object_id
    qury_like = Comment_like.objects.filter(email=email)
    qury_like = Comment_like.objects.filter(email=email, object_id=qury.id, dislike_comment="1").first()
    if qury_like:
        qury_like.delete()
    save_form = Comment_like(email=email, user_name=request.session.get('name'),
                                content_type=qury.content_type,
                                object_id=qury.id,
                                content=qury.content,
                                flag_reply=qury.flag_reply,
                                flag_comment_id=qury.flag_comment_id,
                                flag_level=qury.flag_level,
                                like_comment='1')
    save_form.save()
    qury.like_comment = '1'
    qury.dislike_comment = '0'
    qury.save()
    # return redirect(request.META.get('HTTP_REFERER'))
    return HttpResponseRedirect(reverse('detail', args=[str(obj_id)]))
```

Derek

unread,
May 5, 2021, 9:10:27 AM5/5/21
to Django users
You'll need to use javascript for this - have a look at https://data-flair.training/blogs/ajax-in-django/ for an example.

Aadil Rashid

unread,
May 5, 2021, 10:08:53 AM5/5/21
to django...@googlegroups.com
You need write Ajax for this functionality to implement

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f44ffafe-9d01-43ac-a7ce-43286551aec4n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages