Open In App

Handling HTML Forms in Django: GET and POST Methods

Last Updated : 03 Nov, 2025
Comments
Improve
Suggest changes
37 Likes
Like
Report

Handling HTML forms is a fundamental part of web development. In Django, forms allow users to submit data to the server for processing, whether saving it to a database or retrieving information, while leveraging Django’s built-in form handling capabilities.

HTML Form

An HTML form is a collection of input elements wrapped inside <form>...</form> tags. Forms let users:

  • Enter text
  • Select options
  • Interact with checkboxes, radio buttons, or dropdowns
  • Submit data to the server for processing

The server then handles this data, often involving database operations or custom logic. Django fully supports HTML forms and simplifies both rendering and processing of form data using views.

Understanding GET and POST Methods

1. GET Method

  • Appends form data to the URL as query parameters.
  • Ideal for search forms or filters, where data retrieval doesn’t modify the server state.

Example url:

https://docs.djangoproject.com/en/5.2/search/?q=forms

2. POST Method

  • Sends form data in the HTTP request body.
  • Used when submitting data that modifies the server, like updating a database.
  • Requires CSRF protection in Django using {% csrf_token %}.

Rendering a Simple HTML Form in Django

Consider a project named 'geeksforgeeks' having an app named 'geeks'.

Step 1: Create the HTML Form

Create home.html in geeks/templates/:

HTML
<form action="" method="get">
    <label for="your_name">Your name: </label>
    <input id="your_name" type="text" name="your_name">
    <input type="submit" value="OK">
</form>

This form will send data using the GET method when submitted.

Step 2: Configure URLs

In geeks/urls.py:

Python
from django.urls import path
from .views import home_view

urlpatterns = [
    path('', home_view),
]

Step 3: Create the View

In geeks/views.py:

Python
from django.shortcuts import render

def home_view(request):
    return render(request, "home.html")

Step 4: Run the Server

python manage.py runserver

Handling GET Request Data in Views

Submitting the form will append data to the URL, which can be accessed in the view using request.GET.

Python
from django.shortcuts import render

def home_view(request):
    print(request.GET)  # Prints the submitted data as a QueryDict
    return render(request, "home.html")

After entering a name and submitting the form, the data will be printed in the terminal:

GETmethod
GET Request

request.GET returns a query dictionary that one can access like any other python dictionary and finally use its data for applying some logic.

Handling POST Requests

To use POST instead of GET, modify form in home.html:

HTML
<form action="" method="POST">
    {% csrf_token %}
    <label for="your_name">Your name: </label>
    <input id="your_name" type="text" name="your_name">
    <input type="submit" value="OK">
</form>

Django requires the {% csrf_token %} template tag inside all POST forms for security to prevent Cross-Site Request Forgery attacks.

Update the View to Handle POST Data

Modify view to handle POST data:

Python
from django.shortcuts import render

def home_view(request):
    if request.method == "POST":
        print(request.POST)  # Prints the POSTed data as a QueryDict
        name = request.POST.get('your_name')
    return render(request, "home.html")

Submit the form it shows the data:

POSTmethod
POST Request

This way one can use this data for querying into the database or for processing using some logical operation and pass using the context dictionary to the template.

HTML Forms vs. Django Forms

HTML FormsDjango Forms

Standard forms written in HTML using <form> tags.

Abstracted forms defined as Python classes in Django.
Data handling and validation must be manually implemented in the view.Provides built-in data validation, rendering, and handling.
No direct integration with Django models.Can be linked directly to Django models using ModelForm.
Suitable for simple forms or quick prototypes.Ideal for robust, secure, and reusable form handling.
Form fields are defined in HTML with attributes like type, name, and required.Form fields are defined in Python using Django’s forms module.

Handling Post Request in Django

Explore