Session problem of Django

blog"s article like function, like once + 1, use session to record the current user
problem point is: if you like article 1, article 2 will indicate that it has been supported. The reason is to judge that session
how to realize that articles cannot be liked repeatedly

View Code

class LikeNumView(View):
    def post(self, request):
        if request.session.get("has_like", False):
            return HttpResponse("{"status":"fail", "msg":""}", content_type="application/json")
        comments_id = request.POST.get("comments_id", "")
        blog = Blog.objects.get(id=int(comments_id))
        blog.like_number += 1
        blog.save()
        request.session["has_like"] = True
        return HttpResponse("{"status":"success", "msg":""}", content_type="application/json")

ajax Code
function like () {

            $.ajax({
                cache: false,
                type: "POST",
                url: "{% url "like" %}",
                data: {"comments_id":{{ comments.id }}},
                async: true,
                beforeSend:function(xhr, settings){
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success:function (data) {
                    if (data.status=="fail"){
                        layer.msg("")
                    }
                    else {
                        layers.msg("")
                    }
                }
            })
        }
Feb.26,2021

you can choose
1 and make a like record table. The ID, like time of ID, article is
2, and the like record is stored in redis. The key value is the user ID+ article ID, value is the like time

.
Menu