Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Django - The included urlconf doesn't have any patterns in it

My website, which was working before, suddenly started breaking with the error

ImproperlyConfigured at / The included urlconf resume.urls doesn't have any patterns in it

The project base is called resume. In settings.py I have set

ROOT_URLCONF = 'resume.urls'

Here's my resume.urls, which sits in the project root directory.

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^resume/', include('resume.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),

    (r'^accounts/login/$', 'django.contrib.auth.views.login'),


    #(r'^employer/', include(students.urls)),

    (r'^ajax/', include('urls.ajax')),
    (r'^student/', include('students.urls')),
    (r'^club/(?P<object_id>d+)/$', 'resume.students.views.club_detail'),
    (r'^company/(?P<object_id>d+)/$', 'resume.students.views.company_detail'),
    (r'^program/(?P<object_id>d+)/$', 'resume.students.views.program_detail'),
    (r'^course/(?P<object_id>d+)/$', 'resume.students.views.course_detail'),
    (r'^career/(?P<object_id>d+)/$', 'resume.students.views.career_detail'),

    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'C:/code/django/resume/media'}),

)

I have a folder called urls and a file ajax.py inside. (I also created a blank init.py in the same folder so that urls would be recognized.) This is ajax.py.

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^star/(?P<object_id>d+)$', 'resume.students.ajax-calls.star'),
)

Anyone know what's wrong? This is driving me crazy.

Thanks,

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

TL;DR: You probably need to use reverse_lazy() instead of reverse()

If your urls.py imports a class-based view that uses reverse(), you will get this error; using reverse_lazy() will fix it.

For me, the error

The included urlconf project.urls doesn't have any patterns in it

got thrown because:

  • project.urls imported app.urls
  • app.urls imported app.views
  • app.views had a class-based view that used reverse
  • reverse imports project.urls, resulting in a circular dependency.

Using reverse_lazy instead of reverse solved the problem: this postponed the reversing of the url until it was first needed at runtime.

Moral: Always use reverse_lazy if you need to reverse before the app starts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...