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): name=models.CharField(max_length=30) email=models.EmailField() phone=models.IntegerField() address=models.CharField(max_length=100)
Django admin
In order to make your models work you need to register it in the Django admin (admin.py). 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 admin.site.register(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 settings.py 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> <html> <head> <title>Registration form</title> </head> <body> <div class="main-block"> <div class="left-part"> <i class="fas fa-graduation-cap"></i> <h1>Submit Details for Registration</h1> </div> <form action="" method="POST"> {% csrf_token %} <div class="title"> <i class="fas fa-pencil-alt"></i> </div> <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"> </div> <button type="submit" href="/">Submit</button> </form> </div> </body> </html>
views.py
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": post=register() post.name=request.POST['name'] post.email=request.POST['email'] post.phone=request.POST['phone'] post.address=request.POST['address'] post.save() return render(request, 'registration.html') else: return render(request, 'registration.html')
urls.py
The urls.py of your Django app directs which url will access which view function in your views.py
from django.urls import path from . import views urlpatterns=[ path('', views.registration, name="register") ]
But before we execute this, we need to run two important commands in the terminal
python manage.py makemigrations python manage.py 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 manage.py runserver
As, we can see our form is loading perfectly with the required input fields and the submit button.
Inserting the respective form data, we click submit.
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.
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