Django – How to add multiple submit button in single form?
Last Updated :
12 Dec, 2021
Prerequisites:
When we submit the data using the HTML form, it sends the data to the server at a particular URL which is defined in the action attribute. To perform different actions with a single HTML form, we just need to add multiple submit buttons in the form. For example, If you go through the code of the newsletter app, you will find subscribe and unsubscribe buttons in a single HTML form but both perform different actions.
In this tutorial, we are going to make a newsletter app using Django. We will add subscribe and unsubscribe buttons in a single HTML form. Moreover, We will add or remove the email address of the user from our database according to the user clicks on the subscribe or unsubscribe button.
To create the simple newsletter Django app, follow the below steps.
Create a new Django project
To start a new Django project, your local computer should be satisfied with the prerequisites are given above. Users can use the below command to start a new project.
django-admin startproject newslatter
Next, Go to the project directory.
cd newslatter
Start a newsletter app
To start a new app inside the newsletter project, run the below command inside the project directory.
django-admin startapp appnewslatter
Now, add “appnewslatter” inside the installed_apps section in settings.py of the newsletter project.
path: newslatter/settings.py

Next, Edit the urls.py inside the newsletter folder and add the below code.
Path: newslatter/urls.py
Filename: urls.py
Python3
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path( 'admin/' , admin.site.urls),
path("", include( 'appnewslatter.urls' )),
]
|
Create a new urls.py file inside the “appnewslatter” folder. Now, your project directory should look like the below image.

Edit the urls.py inside the appnewslatter folder. We are adding the URL for the home page.
Path: appnewslattere/urls.py
Filename: urls.py
Python3
from django.urls import path
from . import views
urlpatterns = [
path("", views.home, name = 'home' ),
]
|
Create a “templates” folder inside the appnewslatter folder to store the HTML templates.
path: appnewslatter/templates
Create a news.html file inside the templates folder to add a form for our newslatter app.
Path: appnewslatter/templates/news.html
Add the below code inside the news.html file which includes a single form with 2 submit buttons. Every submits button has a unique name. In the views.py file, We will identify from which button users have sent the post request using the name of the button.
Filename: news.html
HTML
<!DOCTYPE html>
< html >
< head >
< title >NewsLatter</ title >
</ head >
< body >
{% if messages %}
< ul class = "messages" >
{% for message in messages %}
< li {% if message.tags %} class = "{{ message.tags }}" {% endif %}>
{{ message }}</ li >
{% endfor %}
</ ul >
{% endif %}
< form action = "" method = "POST" >
< label for = "email" >Enter your email:</ label >
< input type = "email" id = "email" name = "email" />
< br > < br >
< button type = "submit" name = "subscribe" >Subscribe</ button >
< button type = "submit" name = "unsubscribe" >Unsubscribe</ button >
</ form >
</ body >
</ html >
|
Now, we need to create a table in the database to store the email of the users. We will edit the models.py file.
path: appnewlatter/models.py
Filename: models.py
Python3
from django.db import models
class newslatteremail(models.Model):
userEmail = models.EmailField(max_length = 254 )
def __str__( self ):
return self .userEmail
|
Register the created model inside the admin.py file.
Path: appnewslatter/admin.py
Filename: admin.py
Python3
from django.contrib import admin
from .models import newslatteremail
admin.site.register(newslatteremail)
|
After registering the model, we need to migrate it. Users need to run the below 2 commands one by one.
python manage.py makemigrations
python manage.py migrate
Now, we will edit the views.py file and add the code to handle requests from subscribe and unsubscribe buttons. Here, we check that from which submit button we get the post request with the help of the name attribute of the button.
Syntax:
if 'name_of_button' in request.POST:
# perform some action
Example:
if 'subscribe' in request.POST:
# add the user email in database
if 'unsubscribe' in request.POST:
# remove the user email from database
Copy/paste the below code inside the views.py file.
Path: appnewslatter/views.py
Filename: views.py
Python3
from django.shortcuts import render
from django.contrib import messages
from .models import newslatteremail
def home(request):
if 'subscribe' in request.POST:
email = newslatteremail()
email.userEmail = request.POST.get( "email" )
email.save()
messages.info(
request, 'You have successfully subscribed to our newslatter.' )
if 'unsubscribe' in request.POST:
newslatteremail.objects.get(
userEmail = request.POST.get( "email" )).delete()
messages.info(request, 'sorry to see you!!!' )
return render(request, 'news.html' )
|
Finally, we have created a simple newsletter app and learned about how we can add multiple submit buttons in a single HTML form. Let’s run the project and see the output. Users can run the Django app using the below command.
Python manage.py runserver
Output:

Similar Reads
How to create a form using Django Forms ?
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model
3 min read
Automatically filling multiple responses into a Google Form with Selenium and Python
Prerequisite: Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e. Python, Java, C#, etc.. we will be working with Python. S
3 min read
{{ form.as_table }} - Render Django Forms as table
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
{{ form.as_ul }} - Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Django Form submission without Page Reload
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. Itâs free and open source. I
2 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
How to Build a Web App using Flask and SQLite in Python
Flask is a lightweight Python web framework with minimal dependencies. It lets you build applications using Python libraries as needed. In this article, we'll create a Flask app that takes user input through a form and displays it on another page using SQLite. Run the following commands to install F
3 min read
PyQt5 | How to create capsule shaped push button ?
Capsule shape is basically shape which consist of one rectangle and two semi circle is connected to both the ends, in this tutorial we will see how to create these shaped button. Below is normal Push button vs capsule shaped push button. In order to create capsule shape button we have to do the foll
2 min read
Voting System Project Using Django Framework
Project Title: Pollster (Voting System) web application using Django frameworkType of Application (Category): Web application. Introduction: We will create a pollster (voting system) web application using Django. This application will conduct a series of questions along with many choices. A user wil
13 min read
How to Create Form using CGI script
HTML forms are a fundamental part of web development, allowing users to input data and interact with web applications. Web developers often use Common Gateway Interface (CGI) scripts to process and send the data submitted through these forms. In this article, we'll explain what Python CGI scripts ar
3 min read