Handling POST Requests
In this page:
The GET/POST Branch
A view checks request.method to decide whether to show a blank form or process submitted data.
Example: The GET/POST Branch
A view checks request.method to decide whether to show a blank form or process submitted data.
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Processing Valid Data
Once is_valid() passes inside the POST branch, the view can use cleaned_data and then redirect.
Example: Processing Valid Data
Once is_valid() passes inside the POST branch, the view can use cleaned_data and then redirect.
from django.shortcuts import render, redirect
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
return redirect('thank-you')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Redirect After Success
Redirecting after a successful POST prevents the browser from resubmitting the form if the user refreshes the page.
Note: This is called the Post/Redirect/Get pattern.
Example: Redirect After Success
Redirecting after a successful POST prevents the browser from resubmitting the form if the user refreshes the page.
if form.is_valid():
form.save()
return redirect('thank-you')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Not checking request.method == POST before processing form data, so GET requests also try to validate a form.
- Forgetting to redirect after a successful POST, letting a page refresh accidentally resubmit the form.
- Not re-rendering the bound form with errors when validation fails, silently discarding what the user typed.
- A single view usually handles both GET (show empty form) and POST (process submitted form).
- The if request.method == POST pattern branches between showing and processing the form.
- Redirect after a successful POST (the Post/Redirect/Get pattern) to avoid duplicate submissions on refresh.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: