def article_like(request): user = request.user if not user: return JsonResponse({"code": 1002, "msg": "用户未登录"}) article_id = request.GET.get('article_id') try: article_id = int(article_id) except: return JsonResponse({"code": 1001, "msg": "参数格式错误"}) a_like = ArticleLike.objects.filter(article_id=article_id) print(user) # 如果喜欢表里面没有数据,就新增 if not a_like.exists(): a_like = ArticleLike() a_like.article_id = article_id a_like.user = user a_like.save() data = { "code": 1, "msg": "添加到喜欢", "is_like": 1, } return JsonResponse(data) else: a_like.delete() data = { "code": 1, "msg": "取消喜欢", "is_like": 0, } return JsonResponse(data)
def check_liked(self, user): return ArticleLike.check(self, user)
def cancel_like(self, user): Article.objects(id=str(self.id)).update_one(dec__like_count=1) ArticleLike.cancel(self, user)
def add_like(self, user): Article.objects(id=str(self.id)).update_one(inc__like_count=1) ArticleLike.create(self, user)