def verify_code(request): client = VerifyCode(request) return client.gen_code()
def verify_code(request): """验证码显示""" client = VerifyCode(request) return client.gen_code()
def set_verify_code(request): """ 显示验证码""" code = VerifyCode(request) code.type = 'world' return code.display()
def post(self, request): # 默认提交失败 data = { 'msg': '评论提交失败,请重试', 'status': 0, } print(request.POST) #打印看一下post提交过来的内容 txtCode = request.POST.get('txtCode') #提取出验证码 v_code = VerifyCode(request) #验证验证码 if not v_code.validate_code(txtCode): #如果验证码验证不通过,则报错返回前端 data = { 'msg': '验证码错啦', 'status': 0, } return JsonResponse(data) # if art_uid: #如果uid对应的文章存在 user = request.user u_name = request.POST.get('u_name') #获取评论用户的名称 txtContent = request.POST.get('txtContent') #获取评论内容 u_code = request.POST.get('u_code') #获取一级评论的uid u_recode = request.POST.get('u_recode') #获取子评论的uid if u_recode: #判断此提交的评论是否为二级评论,如果是则存进数据表 cmt = get_object_or_404(ReCommentModel, u_code=u_recode) #获取子评论的回复对象 re_cmt = ReCommentModel() #获取子评论表的对象 re_cmt.self_reply = cmt #外键关联要回复的子评论对象 re_cmt.to_code = u_code #保存回复的一级评论的uid,定位自己在哪一个一级评论下 re_cmt.to_recode = u_recode #保存回复的二级评论的uid,这里的uid存的是子评论表中的评论的uid re_cmt.txtContent = txtContent #保存回复内容 if user.id: re_cmt.u_name = user.username else: re_cmt.u_name = u_name #保存回复者的名称 re_cmt.save() #这里保存的是回复子评论的内容 data = { 'msg': '评论提交成功', 'status': 1, } return JsonResponse(data) if u_code: cmt = get_object_or_404(CommentModel, u_code=u_code) #外键关联一级评论对象 re_cmt = ReCommentModel() re_cmt.comment = cmt #保存一级评论对象 re_cmt.to_code = u_code #保存一级评论的uid re_cmt.txtContent = txtContent #保存回复内容 if user.id: re_cmt.u_name = user.username else: re_cmt.u_name = u_name # 保存回复者的名称 re_cmt.save() #这里保存的是回复一级评论的内容 data = { 'msg': '评论提交成功', 'status': 1, } return JsonResponse(data) art_uid = request.POST.get('art_uid') # 取评论所在文章的uid,保证评论与文章相关联 note_id = request.POST.get('note_id') photo_id = request.POST.get('photo_id') mall_uid = request.POST.get('mall_uid') level_msg_id = request.POST.get('level_msg_id') comment = CommentModel() if art_uid: self.help_save(request, comment, u_name, txtContent) comment.article = Article.objects.get(uid=art_uid) #保存文章uid comment.save() #这里保存的是一级评论的内容 elif note_id: self.help_save(request, comment, u_name, txtContent) comment.note_id = note_id comment.save() elif photo_id: self.help_save(request, comment, u_name, txtContent) comment.photo_id = photo_id comment.save() elif mall_uid: self.help_save(request, comment, u_name, txtContent) comment.mall_uid = mall_uid comment.save() elif level_msg_id: self.help_save(request, comment, u_name, txtContent) comment.level_msg_id = level_msg_id comment.save() data = { 'msg': '评论提交成功', 'status': 1, } return JsonResponse(data)
def clean_verify_code(self): verify_code = self.cleaned_data.get('verify_code') if not VerifyCode(self.request).validate_code(verify_code): raise forms.ValidationError('验证码错误') return verify_code