Two different submit buttons in same form in Django

牧云@^-^@ 提交于 2020-12-03 07:31:53

问题


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 submit buttons: One button which submits and redirects to objects list page (ListView) and another button which submits and redirects to the object detail page (DetailView)?

I don't know how to do this in a smart way.


回答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:

<input type="submit" name="list" value="Submit and go to list">
<input type="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')


来源:https://stackoverflow.com/questions/26256469/two-different-submit-buttons-in-same-form-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!