def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') content = request.POST.get('content', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and content: answer = Answer() question = Question.objects.get(id=question_id) answer.question_id = question.id answer.content = content answer.user = request.user answer.save() return HttpResponse(json.dumps({ "status": "success", "msg": "回答成功", "id": answer.id }), content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败,请重新回答"}', content_type='application/json')
def save(self): answer = Answer() answer.author = self._user answer.text = self.cleaned_data['text'] answer.question_id = self.cleaned_data['question'] answer.save() return answer
def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') choices = request.POST.get('choices', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and choices: exist_records = Answer.objects.filter(user_id=request.user.id, question_id=int(question_id)) if exist_records: return HttpResponse('{"status":"success","msg":"你已经回答过这个问题"}', content_type='application/json') answer = Answer() question = Question.objects.get(id=question_id) question_standard_choices = QuestionStandardAnswers.objects.get( question_id=question_id).choice_position answer.question_id = question.id answer.content = choices answer.user = request.user if choices != question_standard_choices: answer.correct = False answer.save() return HttpResponse('{"status":"success","msg":"答案错误"}', content_type='application/json') answer.save() return HttpResponse('{"status":"success","msg":"答案正确"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败"}', content_type='application/json')
def save(self): a = Answer() a.text = self.cleaned_data['text'] #a.author = self._user a.author_id = self._user.pk a.question_id = self.cleaned_data['question'] a.save() return a