简体   繁体   English

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

[英]Django - Import Error: No module named *.urls

I'm working through the official Django tutorial and adapting it slightly for my own needs using Django version 1.6.1, Python 2.7.6. 我正在使用官方的Django教程,并使用Django 1.6.1版,Python 2.7.6稍微调整它以满足我自己的需要。

I'm at the point where it has me mapping URLs but I keep getting "No module named customers.urls" errors when there is very clearly a module with an aptly named file within, so I'm really at a loss as to what I'm doing wrong. 我正处于让我映射URL的地步,但是当有一个非常清楚的模块中有一个恰当命名的文件时,我一直得到“没有模块名为customers.urls”的错误,所以我真的不知道是什么我做错了。

My initial thought was that I needed to import something customers-related in the root/urls.py but every combination of import resulted in roughly the same error, and the tutorial did not say to do this. 我最初的想法是我需要在root / urls.py中导入与客户相关的内容,但导入的每个组合都会导致大致相同的错误,并且教程没有说这样做。

ROOT_URLCONF = 'taco.urls' (taco is the name of the project) ROOT_URLCONF ='taco.urls'(taco是项目的名称)

I'm running this using manage.py/runserver so there's no special web server trickery going on that I'm aware of. 我正在使用manage.py/runserver运行它,所以没有特殊的Web服务器技巧,我知道。 I've restarted it several times. 我重启了好几次。

The apps are all properly registered, as the traceback can attest. 应用程序都已正确注册,因为回溯可以证明。

Any pointers as to something I'm overlooking would be appreciated! 关于我正在俯瞰的东西的任何指示将不胜感激!

root/urls.py: 根/ 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)),
)

customers/urls.py: 客户/ urls.py:

from django.conf.urls import patterns, url;

from customers import views;

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

customers/views.py: 客户/ views.py:

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

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

Traceback 追溯

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

In your customers/urls.py: 在您的customers / urls.py中:

Change this: 改变这个:

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

For this: 为了这:

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

Also, make sure you have your __init__.py file in package customers . 另外,请确保在包customers__init__.py文件。 And that INSTALLED_APPS is correctly filled with you app name. 并且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',
)

If taco is the name of the project check that apps are being referenced correctly so in your installed apps you may need the following: 如果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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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