I have a Django form below, I want to save a form by using two buttons: "Save & Add" and "Save & Continue" like in Django Admin.
<form method = 'POST' enctype = 'multipart/form-data'>
{% csrf_token %}
{form | crispy}
<div >
<button type="submit" name="save_add" >Save & Add</button>
<button type="submit" name="save_continue" >Save & Continue</button>
<a href="{% url 'doctor:list_doctor' %}" type="button">Close</a>
</div>
</form>
My code in views.py is as follows:
def addDoctor(request):
if request.method == 'POST':
form = DoctorForm(request.POST, request.FILES)
if form.is_valid():
form=form.save(commit=False)
form.save()
if request.POST.get('save_add'):
messages.success(request,'Doctor is successfully added')
return redirect('doctor:list_doctor')
elif request.POST.get('save_continue'):
return redirect('doctor:add_doctor')
return redirect('doctor:list_doctor')
else:
form = DoctorForm()
context = {
'form':form
}
return render(request,'doctor/add_doctor.html',context)
CodePudding user response:
TRY THIS
if request.POST:
if 'save_add' in request.POST:
messages.success(request,'Doctor is successfully added')
return redirect('doctor:list_doctor')
elif 'save_continue' in request.POST:
return redirect('doctor:add_doctor')