如何覆盖 Django-CMS 模板

How to override Django-CMS templates

settings.py

from .ENV import BASE_DIR, ENV_VAR

# SECURITY WARNING: don't run with debug turned on in production!
# False if not in os.environ
DEBUG = ENV_VAR('DEBUG', default=False)

# SECURITY WARNING: keep the secret key used in production secret!
# If not found in os.environ will raise ImproperlyConfigured error
SECRET_KEY = ENV_VAR('SECRET_KEY')

SITE_ID = 1

# Application definition
INSTALLED_APPS = [
    'djangocms_admin_style',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.admin',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'django.contrib.staticfiles',
    'django.contrib.messages',
    'cms',
    'menus',
    'sekizai',
    'treebeard',
    'djangocms_text_ckeditor',
    'djangocms_style',
    'djangocms_column',
    'djangocms_file',
    'djangocms_googlemap',
    'djangocms_inherit',
    'djangocms_link',
    'djangocms_picture',
    'djangocms_teaser',
    'djangocms_video',
    'reversion',
    'config',
]

MIDDLEWARE_CLASSES = [
    'cms.middleware.utils.ApphookReloadMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware'
]

ROOT_URLCONF = 'urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR('config/templates'),
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.i18n',
                'django.core.context_processors.debug',
                'django.core.context_processors.request',
                'django.core.context_processors.media',
                'django.core.context_processors.csrf',
                'django.core.context_processors.tz',
                'sekizai.context_processors.sekizai',
                'django.core.context_processors.static',
                'cms.context_processors.cms_settings'
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'django.template.loaders.eggs.Loader'
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
    'default': ENV_VAR.db()
}

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True

MEDIA_ROOT = BASE_DIR('media', default='')
MEDIA_URL = '/media/'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR('static')
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.FileSystemFinder',
)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

urls.py

from __future__ import absolute_import, print_function, unicode_literals

from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin

admin.autodiscover()

urlpatterns = [
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
        {'sitemaps': {'cmspages': CMSSitemap}}),
    url(r'^select2/', include('django_select2.urls')),
]

urlpatterns += i18n_patterns(
    '',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('cms.urls')),
)

目录结构

.
|-- apps
|   |-- __init__.py
|-- config
|   |-- __init__.py
|   |-- settings
|   |   |-- base.py
|   |   |-- configurations
|   |   |   |-- DJANGOCMS.py
|   |   |   |-- DJANGO.py
|   |   |   |-- ENV.py
|   |   |   |-- GRAPPELLI.py
|   |   |   |-- __init__.py
|   |   |-- dev.py
|   |   |-- __init__.py
|   |   `-- pro.py
|   |-- templates
|   |   |-- base.html
|   |   |-- feature.html
|   |   |-- menu.html
|   |   |-- page.html
|   |-- wsgi.py
|-- manage.py
|-- media
|-- requirements
|   |-- base.txt
|   |-- dev.txt
|   `-- pro.txt
|-- requirements.txt
|-- static
|-- urls.py

我无法打开 localhost:8000/ 它总是将我重定向到登录页面。然后我创建了一个新项目,这次保留了默认项目布局,现在这就是我打开 localhost:8000/

时看到的内容

所以如果有人能告诉我哪里出了问题??如何使用自定义模板???

首先,如果它将您重定向到登录页面,那很可能是因为还没有任何内容。基于这个问题,我假设这是一个没有页面的新安装,所以你需要登录,创建你的页面,然后你应该在没有管理员重定向的情况下加载它。

就覆盖模板而言,它的工作方式与任何 django 或应用程序模板相同。

如果您想使用自定义模板,只需按照与原始模板相同的路径创建自己的版本,但要在您自己的模板目录之一中。

例如,我在我的项目中有一个目录,我在其中覆盖了 CMS 模板;

/project/myapp/templates/cms/toolbar/plugin.html

您可以在 CMS 应用程序中看到的内容位于相同的模板路径中;

https://github.com/divio/django-cms/tree/release/3.3.x/cms/templates/cms/toolbar

如果您有模板并希望将其用作 CMS 的页面模板,那么可以像这样添加一个 CMS_TEMPLATES 设置;

CMS_TEMPLATES = (
    ('home.html', 'Homepage'),
    ('article.html', 'Article'),

    ('blogs/entry_form.html', 'Blogs Competition Entry Form'),
)