def post(self, request, t_pk): form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.title = ForumThread.objects.get(pk=t_pk) answer.created_by = request.user answer.save() return self.get(request, t_pk) else: return self.get(request, t_pk)
def post(self, request, *args, **kwargs): self.object = self.get_object() form = self.get_form() answer_form = AnswerForm(self.request.POST, instance=self.object.answers.first()) if form.is_valid() and answer_form.is_valid(): self.form_valid(form, answer_form) return HttpResponseRedirect(self.get_success_url()) else: return self.render_to_response( self.get_context_data(form=form, answer=answer_form))
def view_question(request,slug): question = get_object_or_404(Question, slug__exact=slug) if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.user = request.user answer.save() question.answers.add(answer) form = AnswerForm() return render(request, 'view_question.html', { 'question':question, 'form':form })
def edit_answer(request, answer_id): try: a = Answer.objects.get(id=answer_id) except Answer.DoesNotExist: raise Http404 if request.method == 'POST': answer_form = AnswerForm(request.POST, instance=a) if answer_form.is_valid(): answer_form.save() return HttpResponseRedirect( reverse('question_page', kwargs={"question_id": a.question.id})) else: answer_form = AnswerForm(instance=a) return render(request, 'forum/edit_answer.html', {'answer_form': answer_form})
def new_answer(request, question_id): try: q = Question.objects.filter(published='P').get(id=question_id) except Question.DoesNotExist: raise Http404 user = request.user if user.is_authenticated(): if request.method == 'POST': answer_form = AnswerForm(request.POST) if answer_form.is_valid(): answer = answer_form.save(commit=False) answer.published = 'P' answer.question = q answer.user = user user.profile.change_star(settings.STAR_RULES['ANSWERING']) answer.save() return HttpResponseRedirect( reverse('question_page', kwargs={"question_id": q.id}))