๐ Introduction
In Django, forms are used to collect input from users and process it securely.
๐ In simple terms:
Form = Interface to take user input and send it to the server
Django provides:
- Built-in form handling
- Validation
- Security (CSRF protection)
๐ฏ Why Forms are Important (Exam Point)
- Used to collect user data (login, signup, feedback)
- Supports GET and POST methods
- Provides automatic validation
- Prevents security issues
๐ Exam Tip:
Forms are handled using:
forms.py- Templates (HTML)
- Views (processing logic)
๐งฉ Types of Forms in Django
1. Simple HTML Forms
2. Django Forms (forms.Form)
3. Model Forms (forms.ModelForm)
๐น 1. Creating a Simple Django Form
Step 1: Create forms.py
from django import forms
class StudentForm(forms.Form):
name = forms.CharField(max_length=100)
age = forms.IntegerField()
Step 2: Create View
from django.shortcuts import render
from .forms import StudentForm
def student_form(request):
form = StudentForm()
return render(request, 'form.html', {'form': form})
Step 3: Create Template (form.html)
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
๐ GET vs POST (Very Important for Exams)
| Feature | GET | POST |
|---|---|---|
| Data visibility | Visible in URL | Hidden |
| Security | Less secure | More secure |
| Usage | Fetch data | Submit data |
| Length limit | Limited | No limit |
๐ Exam Point:
- GET โ retrieving data
- POST โ sending sensitive data
๐น Handling POST Request
Modify view:
def student_form(request):
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
age = form.cleaned_data['age']
return render(request, 'success.html', {'name': name})
else:
form = StudentForm()
return render(request, 'form.html', {'form': form})
๐ CSRF Token (Very Important)
{% csrf_token %}
๐ Why?
- Prevents hacking (Cross-Site Request Forgery)
- Mandatory in POST forms
๐น Model Form (Advanced + Important)
Used when form is directly linked to model.
Example:
from django import forms
from .models import Student
class StudentModelForm(forms.ModelForm):
class Meta:
model = Student
fields = ['name', 'age']
View:
def student_model_form(request):
if request.method == 'POST':
form = StudentModelForm(request.POST)
if form.is_valid():
form.save()
else:
form = StudentModelForm()
return render(request, 'form.html', {'form': form})
๐งช Real-Life Example
๐ Student Registration Form
- Name
- Age
User fills โ form sends data โ stored in database
โ ๏ธ Common Mistakes (Exam Focus)
โ Forgetting {% csrf_token %}
โ Not checking form.is_valid()
โ Mixing GET and POST
โ Not handling request.method
๐ Viva Questions
Q1. What is Django Form?
๐ A class used to collect and validate user input.
Q2. Difference between GET and POST?
๐ GET shows data in URL, POST hides it.
Q3. What is CSRF?
๐ Security mechanism to prevent unauthorized requests.
Q4. What is ModelForm?
๐ Form directly connected to database model.
Q5. Why use form.is_valid()?
๐ To validate user input before processing.
๐ป Mini Practical Program
# forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=50)
message = forms.CharField(widget=forms.Textarea)
๐ Expected Output
๐ A form with:
- Name input
- Message textarea
- Submit button
๐ฏ Summary (Exam Revision)
- Forms collect user data
- Use GET and POST methods
- POST is more secure
- CSRF token is mandatory
- ModelForm connects form with database
๐ Next Topic
๐ Next: Django CRUD Operations (Create, Read, Update, Delete)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
