SITE_ID = 1
和
(r'', include('django.contrib.flatpages.urls')),
位于urls.py
。
我该怎么做才能解决此错误? Django仍然显示这个错误 - 我用谷歌搜索,我找不到任何东西。
from django.conf.urls.defaults 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'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.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)),
(r'', include('django.contrib.flatpages.urls')),
)
这是最基本的urls.py
。这就是为什么我不必发布代码,但现在是。
它也启用了。由于这是一个新项目,我将展示每个文件。
_ init _ 为空
# Django settings for CMS project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'C:\Documents and Settings\Administrator\Desktop\django-projects\cms/cms.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'o2lqxjp!fg%0xod5g$79alt_*o4&lkw-ncr^30iuqcv(y-44i7'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
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.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.flatpages',
'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',
)
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
最后manage.py
:
#!/usr/bin/env python
from django.core.management import execute_manager
import imp
try:
imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)
import settings
if __name__ == "__main__":
execute_manager(settings)
管理界面工作中的flatpages
我实际上是通过管理员
这是我的数据库的路径:
C:\ Documents and Settings \ Administrator \ Desktop \ django-projects \ cms \ cms.db
当我运行python manage.py syncdb
时,我发现没有错误。
我正在尝试访问
http://localhost:8000/test/
当我输入python manage.py runserver
时,我看不出错误。
管理界面完美无缺。
答案 0 :(得分:6)
当我把手伸向django flatpages时遇到同样的问题。 问题是您的网站ID。
当我尝试从管理网站添加平面网页并将网站添加到我的平面网页时, 站点字段中有example.com,其中SITE_ID = 1(通过django_site表)。
示例:我使用admin site添加了一个平面页面 url =' / about /' site =" localhost:8000' name =" about" content ="这是关于页面"
*如果您正在使用localhost,您可能希望将localhost:8000添加到您的站点。 现在,这个新添加的站点(localhost:8000)将具有SITE_ID = 2。*
您可以通过浏览设置文件中提到的数据库的django_site表来验证这一点。
现在请参阅localhost:8000中的 SITE_ID (这是 django_site 表中的主键)并在设置文件中编辑 SITE_ID 指向此SITE_ID(即localhost:8000的SITE_ID)
多数民众赞成,浏览" localhost:8000 / about /"在您的浏览器中(假设您已在应用https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/中扩展了平面页。)
答案 1 :(得分:4)
我认为错误是,您urls.py
不需要flatpages
。相反,你需要:
添加'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' 到您的MIDDLEWARE_CLASSES设置。
引自Django文档 The flatpages application 。
检查代码和项目后,问题是admin
在编辑或创建平面页时,您必须选择一个或多个要关联的网站。
答案 2 :(得分:0)
为了完整起见,我还有这个错误的另一个来源。 问题是在没有前导斜杠的情况下保存 flatpages url。
如果没有前导斜杠,则抛出此错误的视图会更改 url 所以当我将 'example/' 存储到 db 时。
# This is the view function where the error occurs.
# I stored 'example/' as url to db.
def flatpage(request, url):
if not url.startswith('/'):
# Here the url is manipulated and becomes '/example/':
url = '/' + url
site_id = get_current_site(request).id
try:
# '/example/' does not exist in db:
f = get_object_or_404(FlatPage, url=url, sites=site_id)