Django

Django Forms (GET & POST) with Examples

๐Ÿ“Œ 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)

FeatureGETPOST
Data visibilityVisible in URLHidden
SecurityLess secureMore secure
UsageFetch dataSubmit data
Length limitLimitedNo 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
  • Email

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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *