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

Introduction to Django Forms

A Django Form is like a paper checklist the computer builds for you, so a person filling it out can't skip a question or type nonsense.
Syntax
markup
from django import forms

class FormName(forms.Form):
    field_name = forms.CharField()

def view_name(request):
    form = FormName()
    return render(request, 'template.html', {'form': form})

Why Use Django Forms

Django Forms are Python classes that describe what fields a form has, generate the HTML automatically, and validate submitted data before your view code touches it.

Note: Think of a Form class as the single source of truth for a form's fields.

Example: Why Use Django Forms

Django Forms are Python classes that describe what fields a form has, generate the HTML automatically, and validate submitted data before your view code touches it.

markup
from django import forms


class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)
{# 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. #}

Using a Form in a View

A view creates a form instance, passes it to a template, and later re-creates it with submitted data to validate.

Note:
  • An empty ContactForm() renders blank fields
  • ContactForm(request.POST) is bound to submitted data.

Example: Using a Form in a View

A view creates a form instance, passes it to a template, and later re-creates it with submitted data to validate.

markup
from django.shortcuts import render


def contact(request):
    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. #}

Rendering the Form

Django can render every field of a form automatically with a single template variable, wrapping each field in paragraph tags.

Example: Rendering the Form

Django can render every field of a form automatically with a single template variable, wrapping each field in paragraph tags.

markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Send</button>
</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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Building raw HTML forms by hand instead of using Django's Form class, losing built-in validation and cleanup.
  2. Forgetting that a Form object only holds data after you construct it with request.POST.
  3. Assuming a form is valid just because it rendered on the page, without calling is_valid().
Chapter Summary
  • Django's forms app separates form definition (fields), rendering (HTML), and validation (cleaning) into one reusable class.
  • A Form class lives in forms.py and mirrors the structure of a model or plain input set.
  • Forms save you from writing repetitive HTML and manual validation code by hand.

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.