def comment(self, request): # 读取请求 user_id = request.data.get('user_id') photo_id = request.data.get('photo_id') comment_text = request.data.get('comment_text') # 查询数据库 res = {'status': 0, 'msg': ''} try: user = User.objects.get(account=user_id) except User.DoesNotExist: user = None if not user: res['status'] = 401 res['msg'] = '账号不存在' return JsonResponse(res) try: photo = Photo.objects.get(photo_id=photo_id) except Photo.DoesNotExist: photo = None if not photo: res['status'] = 401 res['msg'] = '图片不存在' return JsonResponse(res) # 存入数据库 comment = Comments(comment=comment_text) comment.account = user comment.photo_id = photo comment.save() return JsonResponse(res)