博客的文章点赞功能,点赞一次+1,使用session记录当前用户
问题点就是:在文章1进行点赞,文章2点赞就会提示已支持过了,原因判断出已存在session
如何实现文章不能重复点赞
View代码
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代码
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("感谢你的支持")
}
}
})
}
相关分类