Django – Submit Form data with Post Method

In this tutorial, we will be learning how to submit form data using POST in Django. Submitting forms is an important aspect of any website, be it customer reviews, contact pages or etc. To submit forms in Django we need two things, a database (provided sqlite3 inbuilt in Django) and a model. We’ll look at these step by step.

Django Models

Django models form the basic structure of your table in the database. It allows for forming the table, its datatypes, and constraints. For this example, we will be using a basic registration form consisting of name, email, phone, and address.

from django.db import models

class register(models.Model):

Django admin

In order to make your models work you need to register it in the Django admin ( The reason being is, Django admin reads the metadata from the models and sets up a site area of create, delete, update fields in the model.

from django.contrib import admin
from .models import register

HTML form

Next, we will create a basic HTML form in the template folder in your project directory. Make sure you have that folder listed in your templates directory in file. The form contains some input fields and has a POST method, which means we are submitting data to the server.

We are saving the file as registration.html

<!DOCTYPE html>
    <title>Registration form</title>
    <div class="main-block">
      <div class="left-part">
        <i class="fas fa-graduation-cap"></i>
        <h1>Submit Details for Registration</h1>
      <form action="" method="POST">
        {% csrf_token %}
        <div class="title">
          <i class="fas fa-pencil-alt"></i> 
        <div class="info">
          <input class="fname" type="text" name="name" placeholder="Full name">
          <input type="text" name="email" placeholder="Email">
          <input type="text" name="phone" placeholder="Phone number">
          <input type="text" name="address" placeholder="Address">
        <button type="submit" href="/">Submit</button>

Django views are one of the vital participants of Django’s MVT architecture. We need the code our view function for it to not only return the webpage in response when the URL is requested but, also get the form data the user has inputted and insert those in the database.

from django.shortcuts import render
from .models import register

def registration(request):
    if request.method=="POST":
        return render(request, 'registration.html')
        return render(request, 'registration.html')

The of your Django app directs which url will access which view function in your

from django.urls import path
from . import views

    path('', views.registration, name="register")

But before we execute this, we need to run two important commands in the terminal

python makemigrations
python migrate

These are responsible for applying your models and their changes to the database.

makemigrations – It is responsible for packaging our respective models into migration files.

migrate – Is responsible for making those to the database.


Now, we will run the python codes to check if our form is working properly or not.

python runserver

As, we can see our form is loading perfectly with the required input fields and the submit button.

Django - Submit Form data with Post Method

Inserting the respective form data, we click submit.

inserting form data in Django

After hitting submit we’ll see that the values have been submitted and the page has returned to the same URL once again. Now, to check whether our data has been successfully submitted to the database or not, we have to check the Django admin site.

Go to localhost/admin and you will find your database under your myapp section. On clicking it, you’ll see there are objects by the name of your database, for example – database object (1), these are your inserted data. Select anyone and you’ll see your entered values.

Django - Submit Form data with Post Method

So this is how you submit form data using post in Django.

Hope you liked this tutorial, you can check out my other posts and tutorials –

Create cookies in Django
How to Lookup Dictionary Value with Key in Django Template

Leave a Reply

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