def answer(request, id_question): if request.method == "POST": a_form = AnswerForm(request.POST) if a_form.is_valid(): question = Question.objects.get(pk = id_question) content = request.POST["content"] ans = Answer(content = content) ans.author = request.user ans.question = question ans.save() return HttpResponseRedirect('/qna/question/%d/' % int(id_question))
def answer(request, question_num): c = {} form = AnswerForm(request.POST) question = get_object_or_404(Question, pk=question_num) try: answer = form.save(commit=False) answer.user = request.user answer.question = question answer.save() except ValueError: c['answer_error'] = True return redirect(question)
def post(self, request, **kwargs): question_id = kwargs.get('pk', '') form = AnswerForm(request.POST) if form.is_valid(): obj = form.save(commit=False) if question_id: obj.question_id = question_id obj.author = request.user obj.save() sent = send_email_answer_alert(question_id, request) if sent: obj.sent = True obj.save() return HttpResponseRedirect(f"/question/{question_id}/")
def question(request, question_id): question_instance = Question.objects.get(id= question_id) context = {} context['question'] = question_instance form = AnswerForm(request.POST or None) if form.is_valid(): if request.user.is_authenticated(): print("if") answer_instance = form.save(commit=False) answer_instance.question_name = question_instance answer_instance.answer_by = request.user answer_instance.save() context['success_message'] = "Your answer has been posted!" context['form'] = AnswerForm(None) return render(request, 'body/question.html', context) else: print("else") return redirect("/login") context['form'] = form return render(request, 'body/question.html', context)
def add_answer_to_question(request, pk): #to answe the question q = get_object_or_404(Question, pk=pk) if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.questioanswern = q answer.user = request.user answer.save() return redirect('qna:detail', pk=q.pk) else: form = AnswerForm() return render(request, 'qna/answer_form.html', {'form': form})