Skip to content Skip to sidebar Skip to footer

Two Different Submit Buttons In Same Form In Django

I have an UpdateView in Django. I have just a normal submit button. When the object is updated correctly it redirects to an object list via success_url. Can I make two different su

Solution 1:

Since you're submitting to the same place, and only want to change the redirect destination after save, this is simple. Submit buttons are just like any other input controls in that they have a name and a value, and you receive these in the POST data. So, in your template you can have:

<inputtype="submit" name="list" value="Submit and go to list">
<inputtype="submit" name="detail" value="Submit and go to detail">

and in your view:

if form.is_valid():
    form.save()
    if'list'in request.POST:
        return redirect('list_url')
    else:
        return redirect('detail_url')

Post a Comment for "Two Different Submit Buttons In Same Form In Django"