包含的urlconf manager.urls没有任何模式 [英] The included urlconf manager.urls doesn't have any patterns in it
问题描述
解决方案:找到以下django片段,似乎工作正常
( http://djangosnippets.org/snippets/2445/ )
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)
显然,现在有一个 reverse_lazy 功能在django trunk。
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')
这是不是有效?
如果我不是通用的写这样的东西,它的作品:
If I instead of generic write something like this, it works:
def add_obj(request, pk):
a=reverse('manager-personal-objs')
return HttpResponse(a)
我有一个项目,其中有2个应用程序。每个应用程序都有其URL和视图。他们都工作正常,但在经理的应用程序,一旦我引用反向功能的视图(任何视图),我得到以下错误:
异常类型:不正确的配置
异常值:包含urlconf manager.urls没有任何模式
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
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- package\django\core\urlresolvers.py在_get_url_patterns中,第283行
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
我在整个网站都收到这个错误(编辑:这显然是因为试图导入manager.urls会导致错误)。如果我删除了include manager.urls,一切都恢复了工作;如果我删除了反向的呼叫,一切都很好;如果我尝试将manager.urls重写为一个更简单的版本,它会继续出现错误。
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.
编辑:root urls.py
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都有一个书面工作视图。
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.
推荐答案
问题是您的URLConf尚未完成加载在您的基于类的视图尝试反转URL(AddObjView.success_url)之前。如果您希望在基于类的视图中继续使用反向,您有两个选项:
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)您可以为您的类创建一个get_success_url()方法,并且相反那里
a) You can create a get_success_url() method to your class and do the reverse from there
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
def get_success_url():
return reverse('manager-personal_objs')
b)如果您正在Django的trunk / dev版本上运行,那么可以使用reverse_lazy https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
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
from django.core.urlresolvers import reverse_lazy
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
success_url = reverse_lazy('manager-personal_objs')
选项b是未来版本的Django中首选的方法。
Option "b" is the preferred method of doing this in future versions of Django.
这篇关于包含的urlconf manager.urls没有任何模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!