遵循django教程时发生导入错误:没有名为URL的模块
我正在努力学习Django的教程 我创建了自己的模型,如下所示:遵循django教程时发生导入错误:没有名为URL的模块,django,Django,我正在努力学习Django的教程 我创建了自己的模型,如下所示: from django.db import models class Order(models.Model): name = models.CharField(max_length=30) quantity = models.IntegerField() ROOT_URLCONF = 'lcf.urls' # Python dotted path to the WSGI application used by
from django.db import models
class Order(models.Model):
name = models.CharField(max_length=30)
quantity = models.IntegerField()
ROOT_URLCONF = 'lcf.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'lcf.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'lcf',
)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tsg.views.home', name='home'),
# url(r'^tsg/', include('tsg.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)),
)
my settings.py看起来像:
from django.db import models
class Order(models.Model):
name = models.CharField(max_length=30)
quantity = models.IntegerField()
ROOT_URLCONF = 'lcf.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'lcf.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'lcf',
)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tsg.views.home', name='home'),
# url(r'^tsg/', include('tsg.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.py看起来像:
from django.db import models
class Order(models.Model):
name = models.CharField(max_length=30)
quantity = models.IntegerField()
ROOT_URLCONF = 'lcf.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'lcf.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'lcf',
)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tsg.views.home', name='home'),
# url(r'^tsg/', include('tsg.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)),
)
但是,跑完以后,
python manage.py syncdb
然后跑
python manage.py runserver
浏览器显示以下错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.4.1
Python Version: 2.6.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'lcf')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/staff/nallurv/Envs/tsg/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/home/staff/nallurv/Envs/tsg/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/home/staff/nallurv/Envs/tsg/lib/python2.6/site-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/staff/nallurv/Envs/tsg/lib/python2.6/site-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/home/staff/nallurv/Envs/tsg/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
Exception Type: ImportError at /
Exception Value: No module named urls
我遇到了相同的错误(异常值:没有名为URL的模块),但在我的例子中,是在尝试使用mod_wsgi在Apache下部署我的Django应用程序时。该应用程序在python manage.py runserver上运行良好。事实证明,运行Apache的用户无法读取我的URL.py文件。因此,您可能希望检查url.py上的权限,以确保您登录的同一用户在运行python manage.py runserver
时可以读取它。一种方法是运行命令ls-l
,该命令将显示文件所有者和文件权限。如果这是问题所在,那么让文件所有者(或根用户)运行命令chmod a+r url.py
顺便说一下,你发布的文件的内容看起来很好。除了我们的项目名称不同之外,它与我的项目几乎相同。你能描述一下你的项目结构吗?例如,什么文件在什么包中?即使是一个快速的屏幕截图也可以。你使用的是1.4版本的代码,但是开发教程。不要那样做。丹尼尔·罗斯曼(Daniel Roseman)说,请使用《谢谢你指出这一点》的教程。我换了一个,通过了1.4的测试,但还是没有运气。我似乎在做教程所说的一切。项目结构看起来像:在一个名为“tsg”的目录中,我有两个目录:“lcf”和“tsg”以及“manage.py”。内部“tsg”目录包含:init.py、settings.py、urls.py、wsgi.py。“lcf”目录包含:init.py、models.py、tests.py、views.py。从'lcf'目录中,我修改的唯一文件是'models.py'。我不知道如何在问题中添加屏幕截图。