Skip to content
Justice Tech School

Handling Forms in Views

Check request.method. For POST, bind the data, call is_valid() and use cleaned_data. Redirect after success so a refresh does not resend the form.

Example
def contact(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            print(form.cleaned_data["email"])
            return redirect("thanks")
    else:
        form = ContactForm()
    return render(request, "contact.html", {"form": form})

Exercise

Check validity.

if form.():

Quiz Test

Why redirect after POST?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.