简体   繁体   中英

Django “No Module Named URLs” error

There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tango_with_django_project.views.home', name='home'),
    # url(r'^tango_with_django_project/', include('tango_with_django_project.foo.urls')),

    # 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)),

    url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)

views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Rango says hello world!")

From the settings.py file

ROOT_URLCONF = 'tango_with_django_project.urls'

Hope you all can help get me started

Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),

    url(r'^posts/', include('posts.urls')), ## Custom url include
    url(r'^links/', include('links.urls')), ## Custom url include
)

So then, when you look into the directory structure, you will notice that there are extra two urls.py files

FailBook
|-- posts
   |-- models.py
   |-- urls.py
   |-- views.py
   |-- etc.
|-- links
   |-- models.py
   |-- urls.py
   |-- views.py
   |-- etc.

# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView

urlpatterns = patterns('',

    url(r'^posts/', PostListView.as_view()), 
    url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call

I know this was already solved, but the solutions provided did not help me. When I had this error it was as simple as checking all of the directories that should have had urls.py files.What I discovered was that the urls.py had not been added to the SVN repository that our Django app was pulled from.

I recommend looking in the projectname->projectname->urls.py for all references to app specific urls, and verifying that the urls.py file exists for each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM