62👍
✅
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'))
7👍
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'))
- [Django]-Check if key exists in a Python dict in Jinja2 templates
- [Django]-Difference between APIView class and viewsets class?
- [Django]-Migrating Django fixtures?
Source:stackexchange.com