🛠️ Day 6: Django Forms – Beginner’s Web Dev Series


Yesterday, we learned how to create models and display them using views and templates. Today, you’ll learn how to let users input data into your project — using Django Forms.
Forms may look like regular HTML input fields on the surface, but Django gives you a powerful Pythonic way to define, validate, and manage them.
📍What You’ll Learn
What Django Forms are and why they matter
forms.Form
vsforms.ModelForm
Handling form submission and validation
Rendering forms in templates
Using CSRF tokens and protecting your data
Building your first dynamic form
What Are Forms in Django?
A form is Django’s way of collecting input from users — cleanly and safely.
You can create a form in two ways:
forms.Form
– When you want to define each field manuallyforms.ModelForm
– When you want to generate the form directly from a model
Create a Basic Form – forms.Form
# forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
Handle the Form in Views
# views.py
from django.shortcuts import render
from .forms import ContactForm
def contact_view(request):
form = ContactForm(request.POST or None)
if form.is_valid():
# process form.cleaned_data
print(form.cleaned_data)
return render(request, 'thank_you.html')
return render(request, 'contact.html', {'form': form})
Use the Form in a Template
<!-- templates/contact.html -->
<h1>Contact Us</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Send</button>
</form>
CSRF Token – Your First Security Layer
Django protects every POST form using a CSRF token. Just include {% csrf_token %}
in your form.
Without it, Django will block the request with a 403 error.
Using ModelForm
– Quicker Way with Models
# models.py
class Feedback(models.Model):
name = models.CharField(max_length=100)
feedback = models.TextField()
# forms.py
from django import forms
from .models import Feedback
class FeedbackForm(forms.ModelForm):
class Meta:
model = Feedback
fields = ['name', 'feedback']
# views.py
def feedback_view(request):
form = FeedbackForm(request.POST or None)
if form.is_valid():
form.save()
return render(request, 'thank_you.html')
return render(request, 'feedback.html', {'form': form})
🎨 Styling Forms with Widgets
class StyledForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Your name'
}))
âś… Task for Day 6
Create a Contact Form with:
Name, email, and message fields
Submit button
A thank-you page after submission
Add a new model called
Feedback
Use
ModelForm
to save itShow success message using
messages.success
🚀 What’s Next?
Tomorrow, on Day 7, we will:
Django default authentication
Protected views
Subscribe to my newsletter
Read articles from Shankar Lamichhane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shankar Lamichhane
Shankar Lamichhane
Hi, I’m Shankar — a Sr. Software Engineer specializing in Python, Django, and DevOps. I build scalable web applications, APIs, and cloud-native systems, with a focus on clean architecture and backend automation.