Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django - The included urlconf manager.urls doesn't have any patterns in it

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'^$', 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'),    

)

Exception Location: ...site-packagesdjangocoreurlresolvers.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'^$', 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

class AddObjView(CreateView):
    form_class = ObjForm
    template_name = 'manager/obj_add.html'

    def get_success_url():
        return reverse('manager-personal_objs')

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')

Option "b" is the preferred method of doing this in future versions of Django.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...