Next

Forms Introduction

  • Introduction to Django Forms, handling input fields, widgets, and form rendering process.
  • What are Django Forms?

    Django Forms provide a way to:

    • Create HTML forms using Python

    • Validate user input

    • Handle form submission securely

    Django forms reduce:

    • Manual HTML writing

    • Validation errors

    • Security issues (CSRF, invalid data)

    Why Use Django Forms?

    • Automatic validation

    • Clean and reusable code

    • Protection against bad input

    • Easy form rendering

    Types of Forms in Django

    Form Type

    Description

    Form

    Normal form (no direct DB connection)

    ModelForm

    Connected to Django models

    In this lesson, we focus on Form class.

    Django Form Class

    What is forms.Form?

    A Form class is a Python class used to define form fields and validation rules.

    It lives inside forms.py.

Basic Django Form Class [ forms.py ]

This form collects student information.

from django import forms

class StudentForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    age = forms.IntegerField()
  • Explanation

    Field

    Purpose

    CharField

    Text input

    EmailField

    Email validation

    IntegerField

    Number input

    Using Form in Views

Form Handling in View [ views.py ]

Form Handling in View

from django.shortcuts import render
from .forms import StudentForm

def student_form_view(request):
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            print(form.cleaned_data)
    else:
        form = StudentForm()

    return render(request, 'student_form.html', {'form': form})
  • Explanation

    • request.POST → form data

    • is_valid() → validation check

    • cleaned_data → validated input

    Rendering Forms in Templates

Method 1: Render Entire Form [ student_form.html ]

Render Form Using form.as_p

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>
  • as_p renders fields inside <p> tags.

Method 2: Render Form Using as_table

{{ form.as_table }}

Method 3: Render Form Using as_ul

{{ form.as_ul }}

Method 4: Manual Field Rendering

Custom Form Rendering

<form method="post">
    {% csrf_token %}

    <label>Name:</label>
    {{ form.name }}

    <label>Email:</label>
    {{ form.email }}

    <label>Age:</label>
    {{ form.age }}

    <button type="submit">Submit</button>
</form>
  • Form Validation Flow

    Validation Steps

    User submits form

            ↓

    request.POST

            ↓

    form.is_valid()

            ↓

    cleaned_data

  • GET vs POST in Forms

    Method

    Usage

    GET

    Fetch data

    POST

    Submit sensitive data

    Forms should use POST.

    Real-World Example

Contact Form Example

forms.py

class ContactForm(forms.Form):
    name = forms.CharField(max_length=50)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

views.py

def contact_view(request):
    form = ContactForm(request.POST or None)
    if form.is_valid():
        print(form.cleaned_data)
    return render(request, 'contact.html', {'form': form})
Next