包含的 urlconf manager.urls 中没有任何模式
解决方案:发现以下 django 代码片段似乎工作正常 (http://djangosnippets.org/snippets/2445/)
from django.utils.functional import lazy
from django.core.urlresolvers import reverse
#Workaround for using reverse with success_url in class based generic views
#because direct usage of it throws an exception.
reverse_lazy = lambda name=None, *args : lazy(reverse, str)(name, args=args)
显然,现在有一个 reverse_lazy django trunk 中的函数。
更新:此错误与我在通用视图内调用反向操作有关:
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
success_url = reverse('manager-personal_objs')
这无效吗?
如果我不是通用地写这样的东西,它会起作用:
def add_obj(request, pk):
a=reverse('manager-personal-objs')
return HttpResponse(a)
我有一个包含 2 个应用程序的项目。每个应用程序都有其 url 和视图。它们都工作正常,但是在管理器应用程序上,一旦我在视图(任何视图)中引用反向函数,我就会收到以下错误: 异常类型:配置不当 异常值:包含的 urlconf manager.urls 中没有任何模式
urls 文件
urlpatterns = patterns('',
url(r'^$', ObjView.as_view(), name='manager-obj'),
url(r'^add/$', AddObjView.as_view(), name='manager-add_obj'),
url(r'^personal/$', PersonalObjsView.as_view(), name='manager-personal_objs'),
:)
异常位置:...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
我在整个站点(编辑:这显然会发生,因为尝试导入 manager.urls 将导致错误)。如果我删除 include manager.urls,一切都会恢复正常;如果我删除对反向的调用,一切都很好;如果我尝试将 manager.urls 重写为更简单的版本,则会继续出现错误。
我已经看了很多次了,似乎没有发现什么问题。
edit:root urls.py
# coding=utf8
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.simple import direct_to_template
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# Home Page
url(r'^$', direct_to_template, {'template': 'home.html'}, name="home"),
# manager
url(r'^manager/', include('manager.urls')),
# writing
url(r'^writing/', include('writing.urls')),
)
urlpatterns += staticfiles_urlpatterns()
edit2: 还应该注意的是,url 模板标签在管理器应用程序中工作正常,如果我在其他应用程序上执行反向调用,则反向调用工作正常。 此外,每个 url 都有一个书面的工作视图。
A solution: Found the following django snippet that seems to work fine
(http://djangosnippets.org/snippets/2445/)
from django.utils.functional import lazy
from django.core.urlresolvers import reverse
#Workaround for using reverse with success_url in class based generic views
#because direct usage of it throws an exception.
reverse_lazy = lambda name=None, *args : lazy(reverse, str)(name, args=args)
Apparently, there is now a reverse_lazy function in django trunk.
Update: This error has something to do with me making a call to reverse inside a generic view:
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
success_url = reverse('manager-personal_objs')
Is this not valid?
If I instead of generic write something like this, it works:
def add_obj(request, pk):
a=reverse('manager-personal-objs')
return HttpResponse(a)
I have a project with 2 apps in it. Each app has its urls and views. They both work fine, but on the manager app, as soon as I reference the reverse function in the views(any view), I get the following error:
Exception Type: ImproperlyConfigured
Exception Value: The included urlconf manager.urls doesn't have any patterns in it
The urls file:
urlpatterns = patterns('',
url(r'^
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
# coding=utf8
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.simple import direct_to_template
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# Home Page
url(r'^
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
, ObjView.as_view(), name='manager-obj'),
url(r'^add/
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
, AddObjView.as_view(), name='manager-add_obj'),
url(r'^personal/
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
, PersonalObjsView.as_view(), name='manager-personal_objs'),
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
, direct_to_template, {'template': 'home.html'}, name="home"),
# manager
url(r'^manager/', include('manager.urls')),
# writing
url(r'^writing/', include('writing.urls')),
)
urlpatterns += staticfiles_urlpatterns()
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,在基于类的视图尝试反转 URL (AddObjView.success_url) 之前,您的 URLConf 尚未完成加载。如果您想在基于类的视图中继续使用反向,您有两个选择:
a) 您可以为您的类创建一个 get_success_url() 方法,并从那里执行反向操作
b) 如果您在 Django 的 trunk/dev 版本上运行,那么你可以使用reverse_lazy https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
选项“b”是 Django 未来版本中执行此操作的首选方法。
The problem is that your URLConf hasn't finished loading before your class based view attempts to reverse the URL (AddObjView.success_url). You have two options if you want to continue using reverse in your class based views:
a) You can create a get_success_url() method to your class and do the reverse from there
b) If you are running on the trunk/dev version of Django, then you can use reverse_lazy https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
Option "b" is the preferred method of doing this in future versions of Django.