我有这个奇怪的问题.
当我这样做时:
from django.core.urlresolvers import reverse
reverse('account-reco-about-you')
# returns '/accounts/recommendations/about-you/'
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
# 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'))
Run Code Online (Sandbox Code Playgroud)
如果不相关,我会收到错误消息.它说我的最后一个观点没有找到,就在那里.任何解释?同时,我将使用非反向风格.
Dun*_*kes 12
此问题与尝试在URL准备好被撤消之前在导入时反转某些内容有关.这不是RedirectView本身的问题 - 它会发生在您尝试在urls.py文件中反转的任何内容中,或者可能在由其导入的文件中.
在Django的开发版本中,有一个reverse_lazy专门用于帮助解决这种情况的函数.
如果你使用的是早期版本的Django,这里有一个解决方案:反向Django通用视图,post_save_redirect; 错误'包含urlconf没有任何模式'.
您需要使用Django 1.4及更高版本中"django.core.urlresolvers"中定义的"reverse_lazy".
这是一个示例urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
from django.core.urlresolvers import reverse_lazy
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('apps.website.views',
url(r'^$', 'home', name='website_home'),
url(r'^redirect-home/$', RedirectView.as_view(url=reverse_lazy('website_home')),
name='redirect_home'),
)
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,URL"/ redirect-home"将重定向到"/".希望这可以帮助.
不需要reverse()或reverse_lazy().
只需指定pattern_name参数:
RedirectView.as_view(pattern_name='account-reco-about-you')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7463 次 |
| 最近记录: |