- Learn how to create and process forms in Django.
- Automatically validate form data, ensuring accuracy and security.
- Customize validation using the
cleanmethod for more control. - Display forms in templates easily using Django’s built-in methods.
Introduction to Django Forms: A Beginner's Guide
Published on: 27 November 2025
Last updated on: 30 June 2026

What Are Django Forms?
Django forms are tools that help create and process HTML forms. Instead of writing all the code yourself, Django provides a structured way to define forms and handle the data users submit.
Why Use Django Forms?
- Simplicity: You can create forms with just a few lines of code.
- Validation: Django automatically checks the data users enter, ensuring that email addresses are valid.
- Security: It protects against common web attacks, like Cross-Site Scripting (XSS).
Creating a Simple Form
Here’s an example of a simple form using Django:
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
In this example:
CharFieldis for short text inputs.EmailFieldchecks for valid email addresses.Textareais used for longer text inputs.
Using Forms in a View
After creating a form, use it in a view to handle user input:
from django.shortcuts import render
from .forms import ContactForm
def contact_view(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# Process the data
print(form.cleaned_data)
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
Rendering Forms in a Template
To display the form on a webpage, use Django’s template syntax:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
The {{ form.as_p }} renders the form fields wrapped in <p> tags.
Adding Custom Validation with the clean Method
Django forms allow you to perform additional custom validation by overriding the clean method. This method is automatically called when the form’s is_valid() method is used.
Here’s how you can add custom validation to your form:
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
def clean(self):
cleaned_data = super().clean()
name = cleaned_data.get('name')
message = cleaned_data.get('message')
# Custom validation: Ensure name is included in the message
if name and message and name not in message:
raise forms.ValidationError("The message must include your name.")
return cleaned_data
Explanation:
- The
cleanmethod processes all the fields in the form and ensures the data is valid. cleaned_datacontains the validated data for all fields.- Custom validation logic is applied, and if the data doesn’t meet the criteria, a
ValidationErroris raised.
Using the Form with Validation
When you use the form in your view, the validation happens automatically:
def contact_view(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# Data has passed validation
print(form.cleaned_data)
else:
print(form.errors)
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
Frequently Asked Questions
To show custom error messages, you can use the clean method in your form class. If the data doesn't meet your validation rules, raise a forms.ValidationError with your custom message.
