Identify which submit button was clicked in Django form submit
In Django 2.x there is a slight change to the view method @FallenAngel's answer
if request.method == 'POST':
form = PriceAssessmentSection1(request.POST)
# Note change below
if 'save_home' in request.POST:
return HttpResponseRedirect(reverse('portal_home'))
# Note change below
elif 'save_next' in request.POST: # You can use else in here too if there is only 2 submit types.
return HttpResponseRedirect(reverse('portal_sec2'))
You can give them names. Only clicked buttons send their data with submit. In your template give them appropriate names:
<button type="submit" name="save_home" value="Save&Home"> Save&Home</button>
<button type="submit" name="save_next" value="Save&Next"> Save&Next</button>
And in your view in the related section, you can check which button is clicked by checkng its name.
if request.method == 'POST':
form = PriceAssessmentSection1(request.POST)
if request.POST.get("save_home"):
return HttpResponseRedirect(reverse('portal_home'))
elif request.POST.get("save_next"): # You can use else in here too if there is only 2 submit types.
return HttpResponseRedirect(reverse('portal_sec2'))