Solve the problem that one form corresponds to multiple buttons in Django
- 2021-07-22 10:40:19
- OfStack
Requirements:
In django, sometimes we need to set multiple buttons in a form to achieve different functions.
Solution:
Add different name attributes for different buttons, and then judge the name value in the background. python2 environment, for example:
Our form header
<form method="post" action=" Custom " O nsubmit="return">
Our buttons, such as deleting and updating
<button type="submit" class="btn btn-info" name="del"> Delete </button>
<button type="submit" class="btn btn-info" name="update"> Update </button>
Then different functions are realized through different name
def function(request):
if request.POST:
if request.POST.has_key('update'):
... #update Function realization
else:
... #del Function realization
return render(request, 'xxx.html', yyy)
That is, according to has_key (), judge different buttons and then realize different functions.
In python3, the has_key () method is deleted and replaced with in, as follows:
if 'update' in request.POST: