Python Django“没有模块命名的URL”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20779284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:13:24  来源:igfitidea点击:

Django "No Module Named URLs" error

pythondjango

提问by aliencatbot

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:

已经发布了许多类似的问题,但我已经尝试过这些解决方案无济于事。我正在学习一个基本的 Django 教程,这是我的代码:

urls.py

网址.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

视图.py

from django.http import HttpResponse

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

From the settings.py file

从 settings.py 文件

ROOT_URLCONF = 'tango_with_django_project.urls'

Hope you all can help get me started

希望大家能帮助我开始

采纳答案by user1876508

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

假设我有一个名为 FailBook 的 Django 项目,有两个应用程序、帖子和链接。如果我查看 FailBook/urls.py,我会发现类似

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

那么,当你查看目录结构时,你会注意到有额外的两个 urls.py 文件

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

回答by Joseph Dattilo

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.

我知道这已经解决了,但是提供的解决方案对我没有帮助。当我遇到这个错误时,就像检查所有应该有 urls.py 文件的目录一样简单。我发现 urls.py 没有被添加到我们的 Django 应用程序从中提取的 SVN 存储库中。

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.

我建议在 projectname->projectname->urls.py 中查找对应用程序特定 url 的所有引用,并验证每个 urls.py 文件是否存在。

回答by delicasso

I had this issue while doing a Pluralsight Django tutorial. Two things I noticed:

我在做 Pluralsight Django 教程时遇到了这个问题。我注意到两件事:

1) I was using Django 2.0, and the command url() from Django 1.11 has been replaced with re_path() in 2.0, obtained by importing as follows:

1)我用的是Django 2.0,Django 1.11的命令url()已经替换为2.0的re_path(),导入得到如下:

from django.urls import path, include, re_path 

This replaces,

这取代了,

from django.conf.urls import url, include

see this: Django 2.0 release notesand,

请参阅:Django 2.0 发行说明和,

2) I accidentally called the file to be imported from the subfolder, url.py, not urls.py e.g. rango/url.py not rango/urls.py.

2) 我不小心调用了要从子文件夹 url.py 导入的文件,而不是 urls.py 例如,rango/url.py 不是rango/urls.py。

This was probably the main issue, and all flowed smoothly after that fix.

这可能是主要问题,并且在修复之后一切顺利。