0

我正在尝试遵循https://docs.djangoproject.com/en/dev/intro/tutorial01/上的 Django 教程

我创建了自己的模型,如下所示:

from django.db import models

class Order(models.Model):
    name = models.CharField(max_length=30)
    quantity = models.IntegerField()

我的 settings.py 看起来像:

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',
)

并且 urls.py 看起来像:

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
4

1 回答 1

1

我遇到了同样的错误Exception Value: No module named urls(该应用程序运行良好python manage.py runserver。事实证明,运行 Apache 的用户无法读取我的 urls.py 文件。因此,您可能需要检查 urls.py 的权限,以确保您在运行时登录的同一用户可以读取它python manage.py runserver。一种方法是运行ls -l将显示文件所有者和文件权限的命令。如果这是问题所在,则让文件的所有者(或 root 用户)运行命令chmod a+r urls.py

顺便说一句,您发布的文件的内容看起来还不错。它与我所拥有的几乎相同,只是我们的项目名称不同。

于 2012-09-26T20:24:53.733 回答