Skip to main content

Command Palette

Search for a command to run...

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

Updated
2 min readView as Markdown
🛠️ Day 6: Django Forms – Beginner’s Web Dev Series
S

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.

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 vs forms.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 manually

  • forms.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 it

  • Show success message using messages.success


🚀 What’s Next?

Tomorrow, on Day 7, we will:

  • Django default authentication

  • Protected views


More from this blog

Shankar’s Dev Journal

11 posts

Insights from a Senior Software Engineer building scalable, high-performance systems with Django, DevOps, and Cloud. Tutorials, tips, and real-world development practices — straight from the codebase.