def submit_answer(request): form = AnswerForm(request.POST, instance=Answer(user=request.user)) if not form.is_valid(): return ajax_response(False, extract_all_errors(form)) answer = form.save() action_factory.question_answer(answer) return ajax_response(True, html=render_to_string('main/answer.html', {'answer':answer}, RequestContext(request)))
def get_context_data(self, **kwargs): self.object.viewed += 1 self.object.save() ctx = super(QuestionDetail, self).get_context_data(**kwargs) ctx['answers'] = [] answers = Answer.objects.all_with_score() \ .filter(question=self.object.id) for ans in answers: answer_dict = {} answer_dict['info'] = ans ctx['answers'].append(answer_dict) if self.object.can_accept_answers(self.request.user): ctx['accept_form'] = \ AnswerAcceptanceForm(initial={'accepted': True}) ctx['reject_form'] = \ AnswerAcceptanceForm(initial={'accepted': False}) if self.request.user.is_authenticated: if (QuestionSubscription.objects.is_subscribed( user=self.request.user, question=self.object)): ctx['subscribed'] = True else: ctx['subscribed'] = False vote_data = self.get_vote_data( QuestionVote, self.object, 'question_id', 'qanda:question_vote_create', 'qanda:question_vote_update') vote_form = QuestionVoteForm(instance=vote_data['instance']) ctx['vote_form'] = vote_form ctx['vote_form_url'] = vote_data['url'] i = 0 for ans in answers: answer_dict = {} ans_vote_data = self.get_vote_data( AnswerVote, ans, 'answer_id', 'qanda:answer_vote_create', 'qanda:answer_vote_update') answer_dict['vote_form'] = AnswerVoteForm( instance=ans_vote_data['instance']) answer_dict['vote_url'] = ans_vote_data['url'] ctx['answers'][i].update(answer_dict) i += 1 ctx['answer_form'] = AnswerForm() ctx['answer_form_url'] = reverse('qanda:answer-create', kwargs={ 'question_id': self.object.id}) return ctx
def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx.update({ 'answer_form': AnswerForm(initial={ 'user': self.request.user.id, 'question': self.object.id, }) }) if self.object.can_accept_answers(self.request.user): ctx.update({ 'accept_form': self.ACCEPT_FORM, 'reject_form': self.REJECT_FORM, }) return ctx
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ "answer_form": AnswerForm(initial={ "user": self.request.user.id, "question": self.object.id, }) }) if self.object.can_accept_answers(self.request.user): context.update({ 'accept_form': self.ACCEPT_FORM, 'reject_form': self.REJECT_FORM }) return context
def test_form_with_data(self): form = AnswerForm(data={'answer_text': 'a' * 1000}) self.assertTrue(form.is_valid())