def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) if self.request.POST: context['reply'] = AnswerForm(self.request.POST, instance=self.object.answers.first()) else: context['reply'] = AnswerForm(instance=self.object.answers.first()) return context
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 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}))
def get(self, request, t_pk): form = AnswerForm().as_p() thread = get_object_or_404(ForumThread, pk=t_pk) answers_list = thread.forumanswer_set.all().order_by( '-created_date_time') paginator = Paginator(answers_list, 5) page = request.GET.get('page') answers = paginator.get_page(page) context = {'thread': thread, 'answers': answers, 'form': form} return render(request, 'forum/forum_thread.html', context)
def get_context_data(self, **kwargs): """ Insert discipline into tbl session context. """ context = super(TopicDetailView, self).get_context_data(**kwargs) context['discipline'] = self.get_discipline() context['form'] = AnswerForm() self.increment_views() return context
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 question_page(request, question_id): try: q = Question.objects.filter(published='P').get(id=question_id) except Question.DoesNotExist: raise Http404 answer_form = AnswerForm() q.answer_count = q.answer_set.all().count() q.views = q.views + 1 q.save() answers = Answer.objects.filter(question=q, published='P').order_by('-accepted') if request.user != None: q.is_stared = q.stars.filter(id=request.user.id).exists() for a in answers: a.is_stared = a.stars.filter(id=request.user.id).exists() return render(request, 'forum/question.html', { 'question': q, 'answers': answers, 'answer_form': answer_form })