Django forms
multiple submit buttons
Django development
web forms
Python web development

How can I build multiple submit buttons django form?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A Django form can have multiple submit buttons when the same validated form data should trigger different actions. The usual pattern is to give each button a distinct name or value and then inspect request.POST in the view to determine which action the user chose.

Multiple Buttons In The Template

The HTML side is straightforward. Keep one form, but add more than one submit button.

html
1<form method="post">
2  {% csrf_token %}
3  {{ form.as_p }}
4
5  <button type="submit" name="action" value="save">Save Draft</button>
6  <button type="submit" name="action" value="publish">Publish</button>
7</form>

Only the clicked button contributes its name and value pair to the POST body, which makes the server-side routing simple.

Handling The Action In A Function View

python
1from django.shortcuts import redirect, render
2
3
4def edit_article(request):
5    if request.method == "POST":
6        form = ArticleForm(request.POST)
7        if form.is_valid():
8            article = form.save(commit=False)
9            action = request.POST.get("action")
10
11            if action == "save":
12                article.status = "draft"
13            elif action == "publish":
14                article.status = "published"
15
16            article.save()
17            return redirect("article_detail", pk=article.pk)
18    else:
19        form = ArticleForm()
20
21    return render(request, "edit_article.html", {"form": form})

The important point is that validation happens once. The button only changes what you do with the already-validated data.

Separate Button Names Also Work

Some developers prefer one unique name per button.

html
<button type="submit" name="save_draft">Save Draft</button>
<button type="submit" name="publish_now">Publish</button>

Then the view checks membership directly:

python
1if "save_draft" in request.POST:
2    article.status = "draft"
3elif "publish_now" in request.POST:
4    article.status = "published"

Both approaches are valid. A shared action field with different values often scales a bit better when there are more than two actions.

Class-Based Views

In a class-based view, the idea is the same. Inspect self.request.POST after the form validates.

python
1from django.urls import reverse
2from django.views.generic.edit import CreateView
3
4
5class ArticleCreateView(CreateView):
6    model = Article
7    form_class = ArticleForm
8    template_name = "edit_article.html"
9
10    def form_valid(self, form):
11        response = super().form_valid(form)
12        action = self.request.POST.get("action")
13
14        if action == "publish":
15            self.object.status = "published"
16            self.object.save(update_fields=["status"])
17
18        return response
19
20    def get_success_url(self):
21        return reverse("article_detail", kwargs={"pk": self.object.pk})

The pattern does not change just because the view style changes.

Use Cases Beyond Save Versus Publish

Multiple submit buttons are also useful for workflows such as:

  • previous versus next in a multi-step wizard
  • save versus save-and-add-another in admin-like forms
  • approve versus reject with the same underlying form fields

The form structure can stay the same while the view chooses different post-submit behavior.

Keep Validation And Action Logic Separate

It is tempting to let different buttons bypass validation, but that usually creates inconsistent behavior. In most cases, validate the form first and then branch by action. If one action truly needs different required fields, that is often a sign the workflow should be split into different forms or steps.

Common Pitfalls

The most common mistake is forgetting that only the clicked submit button is included in the POST body. Another is branching on button action before checking form.is_valid(), which can lead to inconsistent save logic. Developers also sometimes duplicate entire forms just to support two actions when a single form plus button routing would be cleaner. Finally, if the buttons should produce very different validation rules, forcing them into one form can make the code harder to reason about.

Summary

  • Multiple submit buttons in Django are usually handled with different button names or values.
  • Inspect request.POST after the form validates to determine which action was chosen.
  • A shared action field with different values is a clean, scalable pattern.
  • The same technique works in both function-based and class-based views.
  • Keep form validation separate from post-submit branching logic.

Course illustration
Course illustration

All Rights Reserved.