def question_detail(request, pk): question = get_object_or_404(Question, pk=pk) context = {'question':question} if request.method == 'POST': answer_form = AnswerForm(request.POST) if answer_form.is_valid(): answer_text = answer_form.cleaned_data.get('text') #creating new answer for this question new_answer = Answer() new_answer.text = answer_text if request.user.is_authenticated(): new_answer.user = request.user new_answer.question = question new_answer.save() #message for successful save context['answer_saved']=True answer_form = AnswerForm() context['answer_form'] = answer_form # return render(request, 'question/detail.html', context) return redirect(request.META['HTTP_REFERER']) # redirect to same url (where form was submitted ) else: answer_form = AnswerForm() context['answer_form']=answer_form return render(request, 'question/detail.html', context)
def QuestionView(request, pk): if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') if request.method == 'POST': form = NewAnswerForm(request.POST) if form.is_valid(): answer = Answer() answer.text=form.cleaned_data['text'] answer.title=form.cleaned_data['title'] answer.user = request.user answer.question=Question.objects.get(pk=int(pk)) answer.save() print "Ok" send_mail('Subject', 'You have new answers', '*****@*****.**', ['*****@*****.**'], fail_silently=False) return HttpResponseRedirect('/question/'+pk+'/#answer'+str(answer.pk)) else: context = {'form':form} context['question']= Question.objects.get(pk=int(pk)) context['answers']= Answer.objects.filter(question__pk=int(pk)) return render_to_response('question.html', context, context_instance=RequestContext(request)) else: ''' ''' form = NewAnswerForm() context = {'form':form} context['question']= Question.objects.get(pk=int(pk)) context['answers']= Answer.objects.filter(question__pk=int(pk)) return render_to_response('question.html',context, context_instance=RequestContext(request))