gpt4 book ai didi

python - Django新手: persistent error when trying to create my first django app

转载 作者:太空宇宙 更新时间:2023-11-03 19:22:37 24 4
gpt4 key购买 nike

我正在通过这本优秀的书“实用 Django 项目”来学习 Django。我能够完美地按照书上的步骤进行操作,但现在我偶然发现了以下问题。创建页面时http://127.0.0.1:8000/first-page/如下图:

enter image description here

我发现以下错误页面:

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

并更改了 TEMPLATE_DIRS settings.py 文件,如下所示:

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

最佳答案

所有错误的答案是这样的(谢谢,mongoose_za):

“在《实用 Django 项目》一书的第 15 页上,您必须确保编辑 example.com 站点而不是添加新站点。您可能已经注意到在 settings.py 中 SITE_ID = 1。如果您添加一个新站点 127.0.0.1:8000,那么该站点的 SITE_ID 将为 2,并且在下面的部分中,默认情况下,当前站点的平面页面 View 过滤器为 1。”

因此,我只需更改 settings.py 中的 SITE_ID = 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/',
)

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

我也遵循了这个建议:

“在 p13 上,与旧版 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

关于python - Django新手: persistent error when trying to create my first django app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9227536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com