Django,教程 - 错误:没有名为urls的模块

时间:2016-09-22 13:39:58

标签: python django

您好我正在按照教程制作Django的第一个网络应用程序(link) 但我得到了这个错误:

 File "c:\Python27\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
   File "c:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
   File "c:\Python27\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
   File "c:\Python27\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
   File "c:\Python27\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
   File "c:\Python27\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
   File "c:\Python27\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
   File "c:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
   File "c:\Python27\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
   File "c:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
   File "c:\Python27\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
   File "c:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
   File "c:\Python27\Proj\mysite\mysite\urls.py", line 7, in <module>
    url(r'^polls/', include('polls.urls'))
   File "c:\Python27\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
   File "c:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name) ImportError: No module named urls

我已经在SO上阅读了一些答案,但他们认为这个错误是由错误版本的框架引起的,这不是我的情况。我的python版本是2.7和Djanog 1.10.1

我的urls.py

from django.conf.urls import url, include


from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls'))    ]

谢谢。

编辑:

文件:polls / urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

编辑2:

感谢大家的评论。我的项目有文件结构。

enter image description here

2 个答案:

答案 0 :(得分:2)

url(r'^polls/', include('polls.urls'))

这条线正在做的是将urls.pypolls app文件夹添加到您的网址,并在所有网址前附加polls/

出现错误:

File "c:\Python27\Proj\mysite\mysite\urls.py", line 7, in <module>
    url(r'^polls/', include('polls.urls'))

该错误表明它无法在urls.py文件夹中找到polls,因此请确保它在那里。并正确配置。

并确保以下内容:

  • 您的应用投票列在INSTALLED_APPS
  • settings.py
  • 确认__init__.py app文件夹
  • 中有polls个文件

答案 1 :(得分:0)

我一直面临这个问题,花了很长时间后,我已经能够理解“没有名为urls的模块”与python依赖项或库无关。

url(r'^polls/', include('polls.urls'))

上面的行显示'polls'是项目的应用程序,Django实际上正在寻找名为'urls.py'的文件。确保使用url matcher文件的确切名称,如app文件夹中所示。

例如,如果文件已命名为“url.py”,请记住将确切的名称写为

url(r'^polls/', include('polls.url'))

与Django 1.10一样,您不必担心其他任何事情。