[Answered ]-Prevent user from submitting likes or dislikes multiple times

2👍

You can store data in user’s session in django easily,

# Returns value of is_user_like key from user's session if it's available, or False
request.session.get('is_user_like', False)
# Sets value of is_user_like in user's session to True
request.session['is_user_like'] = True

Change your template to:

<a href="/like/">
    <button class="like pull-right btn btn-info text-left pad0  btn-mini mrg-left5 like-food">
        <i class="{% if user_likes %}icon-thumbs-down{% else %}icon-thumbs-up{% endif %}"></i>&nbsp;`{{rest.food_likes}}`
    </button>
</a>

And pass the user_likes = request.session.get('is_user_like', False) to template renderer.
Also add store to session line to your like view’s code (And it’s negative to dislike view – if you have any):

def like(request):
    if request.is_ajax:
        r = Restaurant.objects.get(pk=1)
        r.food_likes +=1
        r.save()
        request.session['is_user_like'] = True
        return HttpResponse(str(r.food_likes))
    else:
        return redirect(request.META.get('HTTP_REFERER'))

Look at this document to make sure that you had enabled SessionMiddleware in django – It is enabled by default in generated settings file: https://docs.djangoproject.com/en/dev/topics/http/sessions/

Leave a comment