如果success_url与Django中的模型本身无关,该怎么办? [英] What if success_url is irrelevant with the model itself in Django

查看:404
本文介绍了如果success_url与Django中的模型本身无关,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果success_url由模型确定,我们可以自定义通用视图,如下所示。

If the success_url is determined by the model, we can customize the generic view as below.

def get_success_url(self):
    return '/object/%s' % (self.object.id)

但是如果success_url与模型无关,比如我们要返回页面,然后再进入表单页面。它可以是详细页面,列表页面或其他任何内容。可以将表单中的引荐网址存储吗?任何想法都不胜感激。

But if the success_url is irrelevant with the model, such as, we want to return to the page before we entered the form page. It can be a detail page, a list page, or anything else. Can I store the referral url in the form? Any idea is appreciated.

〜Hanson

推荐答案

成功页面不会总是相同,否则您可以在 get_success_url reverse()的URL >。
对于动态重定向,您可以像建议的一样将URL添加到表单的隐藏输入中,也可以在url的querystring中将表单发布到(例如, / myurl?next = /重定向-URL / )。那么你可以在你的视图类中执行以下操作:

I assume that the success page will not always be the same as otherwise you could just include the url with something like reverse() in get_success_url. For a dynamic redirect you could either, like you suggested add the url in a hidden input to the form or in the querystring of the url the form is posted to (eg. /myurl?next=/redirect-url/). Then you could do in your view class something like:

# url is in the form in a hidden input 'next'
def get_success_url(self):
    return request.POST['next']

# url is in the query string ?next=xxxx
def get_success_url(self):
    return request.GET['next']

这篇关于如果success_url与Django中的模型本身无关,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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