Introduction to Django Forms
In this page:
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.
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.
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.
<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. #}
- Building raw HTML forms by hand instead of using Django's Form class, losing built-in validation and cleanup.
- Forgetting that a Form object only holds data after you construct it with request.POST.
- Assuming a form is valid just because it rendered on the page, without calling is_valid().
- 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.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: