How can I build multiple submit buttons django form?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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
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.
Then the view checks membership directly:
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.
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.POSTafter the form validates to determine which action was chosen. - A shared
actionfield 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.
Related reading
- How can I calculate a rolling / moving average using Python NumPy / SciPy?
- How can I call a function within a class?
- How can I call a shell script from Python code?
- How can I catch multiple exceptions in one line? in the except block
- How can I change the shape of a variable in TensorFlow?
- How can I check for Python version in a program that uses new language features?
- How can I check if a key exists in a dictionary?
- How can I check if a string represents an int, without using try/except?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.