Django RedirectView和reverse()不一起工作? [英] Django RedirectView and reverse() doesn't work together?

查看:121
本文介绍了Django RedirectView和reverse()不一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个奇怪的问题。

当我这样做:

from django.core.urlresolvers import reverse
reverse('account-reco-about-you')
# returns '/accounts/recommendations/about-you/'

但是当我这样做:

# Doesn't Work
recommendations = login_required(RedirectView.as_view(url=reverse('account-reco-about-you')))

# Work
recommendations = login_required(RedirectView.as_view(url='/accounts/recommendations/about-you'))

错误信息如果不相关,我得到。它说我的最后一个视图是没有找到,那是在那里。任何解释?同时,我会用非反向风格做。

Error message I get if unrelated. It says my last view is not found, which is there. Any explanation? Meantime, i'll make do with the non-reverse style.

推荐答案

这个问题是为了扭转一些东西在URL准备好撤销之前的导入时间。这不是RedirectView本身的问题 - 它会发生在您尝试在urls.py文件中反转的任何东西,或者可能在其导入的文件中。

This problem is to do with trying to reverse something at import time before the URLs are ready to be reversed. This is not a problem with RedirectView itself - it would happen with anything where you tried to reverse in your urls.py file, or possibly in a file imported by it.

在Django的开发版本中,有一个名为 reverse_lazy 专门帮助这种情况。

In the development version of Django, there is a function called reverse_lazy specifically to help in this situation.

如果您使用的是早期版本的Django,那么这里有一个解决方案:反向Django通用视图,post_save_redirect;错误'include urlconf没有任何模式'

If you're using an earlier version of Django, there is a solution here: Reverse Django generic view, post_save_redirect; error 'included urlconf doesnt have any patterns'.

这篇关于Django RedirectView和reverse()不一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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