Django - How to add multiple submit button in single form?
Last Updated :
21 May, 2025
We will create a simple Django-based newsletter app that allows users to subscribe and unsubscribe using a single form with multiple submit buttons. By leveraging Django's form handling and CSRF protection, we will capture email inputs and store or remove them from the database based on user interaction.
To create the simple newsletter Django app, follow the below steps:
1. Create a new Django project
Prerequisites:
Run the following command to create a new Django project:
django-admin startproject newsletter
Navigate to the project directory:
cd newsletter
2. Create a New App for the Newsletter
Next, create a new Django app inside your project. The app will handle the logic and templates for the newsletter functionality.
Run the following command to create the app:
django-admin startapp appnewslatter
Create a new urls.py file inside the "appnewslatter" folder. Now, your project directory should look like the below image.
Project DirectoryAfter creating the app, you need to add it to the INSTALLED_APPS list in your settings.py file, which can be found in newsletter/settings.py. Open the settings.py file and add 'appnewsletter' to INSTALLED_APPS:
INSTALLED_APPS = [
# Other apps
'appnewsletter',
]
Now, set up the URLs for the app by editing the urls.py files.
Edit the urls.py in the Project Directory
In newsletter/urls.py, include the URLs of the appnewsletter app:
Python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
#including URLS of appnewslatter app
path("", include('appnewslatter.urls')),
]
Create urls.py Inside the appnewsletter Folder
Next, create a urls.py file inside the appnewsletter directory and add the URL for the homepage.
Python
from django.urls import path
from . import views
urlpatterns = [
# URL to open home page
path("", views.home, name='home'),
]
Inside the appnewsletter app, create a templates folder to store your HTML files. Inside the templates folder, create a news.html file.
Path: appnewsletter/templates/news.html
Add the following code to create a form with two submit buttons, one for subscribing and one for unsubscribing. Both buttons will perform different actions based on the button clicked.
HTML
<!DOCTYPE html>
<html>
<head>
<title>NewsLatter</title>
</head>
<body>
<!--showing success message-->
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<!--Form with multiple submit buttons-->
<form action="" method="POST">
{% csrf_token %}
<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>
Here:
- The form has two submit buttons with different names: subscribe and unsubscribe.
- When a user clicks a button, it sends the corresponding button's name with the form data.
5. Create the Database Model
In the models.py file, define a model to store the email addresses of the subscribers.
Path: appnewsletter/models.py
Python
from django.db import models
# creating database model to store email
class newslatteremail(models.Model):
userEmail = models.EmailField(max_length=254)
def __str__(self):
return self.userEmail
This model has a single field userEmail that stores the email addresses of the users who subscribe to the newsletter.
6. Register the Model in Django Admin
To manage the subscriber emails from the Django admin interface, you need to register the model.
Path: appnewsletter/admin.py
Python
from django.contrib import admin
from .models import newslatteremail
# registering the model
admin.site.register(newslatteremail)
In the views.py file, define the logic to handle form submissions. The home view will check which submit button was clicked using the request.POST dictionary.
Path: appnewsletter/views.py
Python
from django.shortcuts import render
from django.contrib import messages
from .models import newslatteremail
def home(request):
# if post request comes from the subscribe button
# then saving user email in our database
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 post request comes from the unsubscribe button
# then deleting the user email from our database
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')
7. Apply Database Migrations
Now that the model is defined, you need to apply the database migrations to create the table in the database.
Run the following commands:
python manage.py makemigrations
python manage.py migrate
8. Run the Django Development Server
Once you've implemented all the steps, it's time to run the project. Run the Django development server using the following command:
Python manage.py runserver
Output
HomeWhen we enter email and lick on Subscribe button:
Successfully subscribedWhen we again enter the same email and click on Unsubscribe button:
After Unsubscribing
Similar Reads
Handle Multiple Forms on a Single Page in Django
One common scenario in web development is handling multiple forms on a single page. This might be required when a user needs to fill out various types of information without navigating away from the page. In this article, we will explore the best practices for managing multiple forms on a single pag
6 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the met
3 min read
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
MultipleChoiceField - Django Forms
MultipleChoiceField in Django Forms is a Choice field, for input of multiple pairs of values from a field. The default widget for this input is SelectMultiple. It normalizes to a Python list of strings which you one can use for multiple purposes. MultipleChoiceField has one required argument: choice
5 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
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
Add Forms Dynamically In Django Using Formset And JavaScript
Formsets in Django allow us to manage a collection of forms as a single unit. They are particularly useful when we need to handle multiple forms on a single page, like adding or editing multiple objects at once. However, a common requirement in many applications is the ability to add forms to a form
8 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 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
Render HTML Forms (GET & POST) in Django
Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing
3 min read