← Back to Django Course | Chapter 7: Django Forms | Lesson 6 of 11

Handling POST Requests

Handling a POST request means the view checks whether someone just clicked Submit, and if so, processes what they typed.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Not checking request.method == POST before processing form data, so GET requests also try to validate a form.
  2. Forgetting to redirect after a successful POST, letting a page refresh accidentally resubmit the form.
  3. Not re-rendering the bound form with errors when validation fails, silently discarding what the user typed.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.