Skip to content

Instantly share code, notes, and snippets.

@spookylukey
Created June 29, 2016 10:49
Show Gist options
  • Save spookylukey/be03869f55276191a9bdb5bcac4a17bd to your computer and use it in GitHub Desktop.
Save spookylukey/be03869f55276191a9bdb5bcac4a17bd to your computer and use it in GitHub Desktop.
Two forms one view
# Views
def two_form_view(request):
context = {}
if request.method == "POST":
question_form = QuestionForm(request.POST)
answer_form = AnswerForm(request.POST)
success = False
if 'q_button' in request.POST and question_form.is_valid()
question_form.save()
success = Treu
if 'a_button' in request.POST and answer_form.is_valid()
answer_form.save()
success = True
if success:
return HttpResponse(reverse('success'))
else:
question_form = QuestionForm(request.POST)
answer_form = AnswerForm(request.POST)
context['answer_form'] = answer_form
context['question_form'] = question_form
return render(request, 'forms.html', context)
def success(request):
return render(request, 'success.html', {})
# Templates:
forms.html:
<h1>Question Form</h1>
<form action="" method="post">{% csrf_token %}
{{ question_form }}
<input type="submit" name="q_button" value="Send Question">
</form>
<h1>Answer Form</h1>
<form action="" method="post">{% csrf_token %}
{{ answer_form }}
<input type="submit" name="a_button" value="Send Answer">
</from>
success.html:
<h1>Your request has been submitted</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment