Django - 导入错误:没有名为* .urls的模块

时间:2014-09-05 20:44:16

标签: python django

我正在使用official Django tutorial并使用Django 1.6.1版,Python 2.7.6稍微根据自己的需要进行调整。

我正处于映射URL的地步但是当有一个非常明确的模块中有一个恰当命名的文件时,我一直得到“没有名为customers.urls的模块”错误,所以我真的很茫然关于我做错了什么。

我最初的想法是,我需要在root / urls.py中导入与客户相关的内容,但导入的每个组合都会导致大致相同的错误,并且教程没有说这样做。

ROOT_URLCONF ='taco.urls'(taco是项目的名称)

我正在使用manage.py/runserver运行它,所以没有特殊的Web服务器技巧,我知道。我已经多次重启了它。

应用程序都已正确注册,因为回溯可以证明。

任何关于我忽略的事情的指示都会受到赞赏!

根/ urls.py:

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

admin.autodiscover()

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

客户/ urls.py:

from django.conf.urls import patterns, url;

from customers import views;

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

客户/ views.py:

from django.shortcuts import render
from django.http import HttpResponse;

def index(request):
    return HttpResponse("Hello");

回溯

Environment:


Request Method: GET
Request URL: http://192.168.3.208:8000/customers/

Django Version: 1.6.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'south',
 'taco.customers',
 'taco.inventory',
 'taco.lookups',
 'taco.orders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  99.                 resolver_match = resolver.resolve(request.path_info)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  337.             for pattern in self.url_patterns:
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  365.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  360.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/var/project/taco/taco/urls.py" in <module>
  7.     url(r'^customers/', include('customers.urls')),
File "/usr/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)

Exception Type: ImportError at /customers/
Exception Value: No module named customers.urls

2 个答案:

答案 0 :(得分:7)

在您的customers / urls.py中:

改变这个:

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

为此:

urlpatterns = patterns('', 
    url(r'^$', views.index, name='index')
);

另外,请确保您在包__init__.py中包含customers个文件。并且INSTALLED_APPS正确地填充了您的应用名称。

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'south',
    'customers',
    'inventory',
    'lookups',
    'orders',
)

答案 1 :(得分:0)

如果taco是项目的名称,请检查是否正确引用了应用,因此在您安装的应用中,您可能需要以下内容:

Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'south',
 'customers',
 'inventory',
 'lookups',
 'orders')