def post(self, request): result = CommentForm(request.POST) ret = result.is_valid() response = BaseResponse() if ret: blog_id = result.cleaned_data['commentBlog'] blog = Blog.objects.get(pk=blog_id) user = request.session['user'] result.cleaned_data['commentBlog'] = blog result.cleaned_data['commentUser'] = user BlogComment.objects.create(**result.cleaned_data) if not result.cleaned_data['reply']: # 此处不能使用update,这也说明了为什么在使用update的时候,前面必须使用filter而不能使用get了,总结说从使用情境上看,update更加适用于批量数据更新,而save则更适合当然也只适合做单条记录的数据更新操作了,当然从SQL的执行情况来看,使用upate是要优于save方式的 # blog.update(commentTotal=F('commentTotal') + 1) blog.commentTotal = blog.commentTotal + 1 blog.save() response.status = True return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder), content_type='application/json') else: response.message(result.errors.as_data()) return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder), content_type='application/json')
def get(self, request, blog_id): response = BaseResponse() try: Blog.objects.filter(pk=blog_id).update(favorTotal=F('favorTotal') + 1) response.status = True except Exception as e: print(e) return HttpResponse(json.dumps(response.__dict__), content_type="application/json")
def get(self, request, blog_id): response = BaseResponse() try: # update更新时不能使用get方法会报错('Blog' object has no attribute 'update')使用filter方法 Blog.objects.filter(pk=blog_id).update(readTotal=F('readTotal') + 1) response.status = True except Exception as e: print(e) return HttpResponse(json.dumps(response.__dict__), content_type="application/json")
def get(self, request, blog_id): response = BaseResponse() try: blog = Blog.objects.get(pk=blog_id) comment_query_set = BlogComment.objects.filter(commentBlog=blog) comment_query_list = transform_comment(comment_query_set) comment_tree = build_tree(comment_query_list) response.status = True response.data = comment_tree except Exception as e: print(e) return HttpResponse(json.dumps(response.__dict__, ensure_ascii=False), content_type='application/json')
def post(self, request): result = BlogForm(request.POST) ret = result.is_valid() response = BaseResponse() if ret: classification_id = result.cleaned_data['classification'] classification = BlogClassification.objects.get(pk=classification_id) result.cleaned_data['classification'] = classification result.cleaned_data['createUser'] = request.session['user'] Blog.objects.create(**result.cleaned_data) response.status = True return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder), content_type='application/json') else: response.message(result.errors.as_json()) return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder), content_type='application/json')
def valid_username(request): if request.method == 'GET': username = request.GET.get('username') users = User.objects.filter(username=username) # https://code.ziqiangxuetang.com/django/django-queryset-api.html response = BaseResponse() if not users.exists(): response.status = True return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder, ensure_ascii=False), content_type="application/json") else: response.summary = '用户名已存在' return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder, ensure_ascii=False), content_type="application/json")
def post(self, request): result = RegisterForm(request.POST) ret = result.is_valid() response = BaseResponse() if ret: response.status = True return HttpResponse(json.dumps(response.__dict__, ensure_ascii=False), content_type="application/json") else: # 错误返回json print(result.errors) error = result.errors.as_json() # error = result.errors.as_data() response.message = result.errors.as_json() return HttpResponse(json.dumps(response.__dict__, ensure_ascii=False), content_type="application/json")
def post(self, request): remember_me = request.POST.get('rememberMe') result = LoginForm(request.POST) vcode = request.session['CheckCode'] # 如果所有规则都满足,则ret为true,只要有一条不满足,ret就为false。 ret = result.is_valid() # 在此我注释以下代码是因为我在from表单进行校验时,删除了数据中的 验证码的key,在此获取用户输入的信息的时候,会报找不到key的错误 # result.clean() # 就是获取到的用户输入信息,但必须放在is_valid方法后,否则报object has no attribute 'cleaned_data'异常 # data = result.clean() # print(data) if ret: user = result.cleaned_data # print("登陆成功,当前登陆人是:", user.__dict__['username']) print("登陆成功,当前登陆人是:", user['username']) # request.session['user'] = user #这个地方保存的session只是一个字典对象(而且这个字典对象内的key跟实体对象的字段也不一致,不能用来做数据库过滤条件),不能用于反向查询 request.session['user'] = User.objects.get( username=user['username']) if remember_me: # 记住我,设定过期时间在1个月之后 request.session.set_expiry(2592000) # return redirect("/blog/index/") response = BaseResponse() response.status = True # 通过dumps()方法中的cls函数,添加自定义的处理函数,其中 ensure_ascii参数表示序列化时对中文使用哪种编码,默认为True使用ascii编码,而通用的基本使用unicode编码,所以此项设置为False,以避免中文序列化的显示问题 return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder, ensure_ascii=False), content_type="application/json") else: # form.errors: 获取错误信息,表单的错误以字典形式返回(如果有多个错误, 可以循环这个字典, 然后传给前端) print(result) print(result.errors) print(result.errors.as_json()) # return render(request, "login.html", {'err': result}) response = BaseResponse() response.message = result.errors.as_json() return HttpResponse(json.dumps(response.__dict__, cls=JsonCustomEncoder, ensure_ascii=False), content_type="application/json")