Django新手:创建第一个Django应用时遇到持续错误

1 投票
3 回答
503 浏览
提问于 2025-04-17 12:45

我正在学习Django,参考的是一本很棒的书《实用Django项目》。我按照书中的步骤做得很好,但现在遇到了一个问题。

我在创建页面 http://127.0.0.1:8000/first-page/ 时,出现了如下的错误页面:

enter image description here

这个错误页面和书上第16页提到的有点不同:

enter image description here

然后,我打开了urls.py文件,并在里面添加了以下代码:

(r'', include ('django.contrib.flatpages.urls')),

代码现在看起来是这样的:

enter image description here

我保存了urls.py文件,然后再次访问 http://127.0.0.1:8000/first-page/,结果还是出现了同样的错误信息(没有任何变化)。而根据书中的内容,我现在应该看到另一个错误页面:

enter image description here

接着,我创建了一个目录和文件default.html,用于模板,像这样:

enter image description here

并且我把settings.py文件中的TEMPLATE_DIRS设置改成了这样:

enter image description here

但是再次访问 http://127.0.0.1:8000/first-page/ 时,还是显示同样的错误信息,这次应该显示的是:

enter image description here

我已经重复了好几次这些步骤。

我使用的是Python 2.6和Django 1.1(和书中的版本一样)。

有没有人知道我哪里做错了?

非常感谢任何帮助。

这一切看起来都那么简单明了,但就是不管用!

这是我的设置文件代码:

# Django settings for cms project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql',       'sqlite3' or 'oracle'.
DATABASE_NAME = 'C:\Projetos\cms\cms.db'             # Or path to database file if using sqlite3.
DATABASE_USER = ''             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with    sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

TIME_ZONE = 'America/Chicago'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

MEDIA_ROOT = ''

MEDIA_URL = ''

ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = ''

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

ROOT_URLCONF = 'cms.urls'

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.
    'C:/Projetos/templates/',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',         
    'django.contrib.flatpages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
)

3 个回答

0

出现 TemplateDoesNotExist 错误的原因不是你的模板有问题,而是 500.html 文件出了错。其实是你在模板或视图中的代码有问题,导致 Django 返回了一个 500 错误。不过在开发阶段,你应该把 DEBUG = True 设置为真,这样 Django 就会显示错误的详细信息,而不是试图加载 500.html。

所以,简单的解决办法就是把 DEBUG = True 设置好,这样你就能看到真正的错误信息并进行修正。不过,当你最终上线时,还是需要 500.html 文件的,所以不妨提前把它也创建好。

0

首先,出现“模板不存在”的错误是因为你没有这个模板。

具体来说,就是在这个路径下找不到:C:/Projects/templates/flatpages/default.html(而且在你的截图中,模板目录拼写成了“Projetos”)。

其次,你不需要为平面页面(flatpages)添加任何网址规则。平面页面是通过中间件来工作的:

'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',

所以在你的设置文件中:

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'django.middleware.common.CommonMiddleware',
#'debug_toolbar.middleware.DebugToolbarMiddleware',
)
1

所有错误的解决办法就是这个(感谢 mongoose_za):

“在《实用 Django 项目》这本书的第 15 页,你必须确保编辑 example.com 网站,而不是添加一个新的网站。你可能在 settings.py 文件中注意到了 SITE_ID = 1。如果你添加一个新的网站 127.0.0.1:8000,那么它的 SITE_ID 就会是 2,而接下来的 flatpage 视图默认是根据当前网站过滤的,而当前网站的 ID 是 1。”

所以,我在 settings.py 中把 SITE_ID = 2 改成了 2(而不是 1),因为我添加了一个新的网站 127.0.0.1:8000。

之后,我根据书中的说明,把我的模板文件夹的路径改成了:

    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.
    'C:/Projetos/templates/',
)

我得到了预期的结果(空白页面“我的第一页”)

我还遵循了这个建议:

“在第 13 页,第一次偏离旧版 Django 的地方。在你的 settings.py 中添加 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',(别忘了逗号)到你的 MIDDLEWARE_CLASSES。”

所有这些信息都来自这个很棒的博客:

http://blog.haydon.id.au/2008/08/2-your-first-django-site-simple-cms.html

撰写回答