在Django中,两种不同的提交按钮具有相同的形式 [英] Two different submit buttons in same form in Django

查看:139
本文介绍了在Django中,两种不同的提交按钮具有相同的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个UpdateView.

I have an UpdateView in Django.

我只有一个正常的提交按钮.正确更新对象后,它将通过success_url重定向到对象列表.

I have just a normal submit button. When the object is updated correctly it redirects to an object list via success_url.

我可以制作两个不同的提交按钮:一个按钮提交并重定向到对象列表页面(ListView),另一个按钮提交并重定向到对象详细信息页面(DetailView)吗?

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.

推荐答案

由于您要提交到同一位置,并且只想在保存后更改重定向目标,所以这很简单.提交按钮与其他任何输入控件一样,都具有名称和值,您可以在POST数据中接收它们.因此,在您的模板中,您可以:

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">

并在您看来:

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

这篇关于在Django中,两种不同的提交按钮具有相同的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆