python - 将 Django 指向不同的模板目录

标签 python django templates django-templates django-oscar

我目前正在构建一个基于 Django-Oscar 的 Django 电子商务网站。默认情况下,后者带有基本的 Bootstrap 主题和样式。

我将这些模板文件移至

project_root/templates/oscar

据我了解,如果我要覆盖这些模板,它将覆盖位于 virtualenv 中的原始 Oscar 模板。

下一步是我下载了一个我想用于该网站的不同主题。

特别是,我希望该主题驻留在

project_root/templates/main_theme

问题是我似乎无法让 Django 从该目录中获取模板。如果我删除 project_root/templates/oscar,它似乎会恢复默认的 Oscar 模板。

如果我将 base.html 放入 project_root/templates/ 并使该 base.html 成为新项目的主文件主题,就会显示出来。但是,我仍然想将其分解为较小的文件,并将它们“存在”在单独的目录中。我怎样才能做到这一点?有没有更谨慎的方法来解决这个问题?

以下是一些相关设置:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.flatpages',
    'django.contrib.sites',

    'compressor',
    'widget_tweaks',
] + get_core_apps()

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            OSCAR_MAIN_TEMPLATE_DIR,
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.contrib.messages.context_processors.messages',

                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.promotions.context_processors.promotions',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
        },
    },
]

COMPRESS_ROOT = os.path.join(BASE_DIR, "static")

STATIC_URL = '/static/'

#STATIC_ROOT = os.path.join(BASE_DIR, "static")

#This should be commented out in Development
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, "static"),
]

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

最佳答案

以下代码对我有用。事实证明,需要在 TEMPLATES 列表的 DIR 列表中指定一个附加条目。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),

            #re-route the search for templates into this custom template directory
            os.path.join(os.path.join(BASE_DIR, 'templates'), 'tshirt-theme'),

            #Uncomment the line below to restore the original Oscar template
            #OSCAR_MAIN_TEMPLATE_DIR,
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.contrib.messages.context_processors.messages',

                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.promotions.context_processors.promotions',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
        },
    },
]

关于python - 将 Django 指向不同的模板目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317177/

相关文章:

Django:在查询上使用 .distinct() 会导致无效的数据库查询

php - 更改 WooCommerce 中的结帐页面

c++ - 什么时候执行类型参数的类型检查?

c++ - 类似类型列表(?)的模板类

Python pandas 按多个索引范围切片数据框

python - 使用 BS4 中的 findAll 创建列表

Django 自定义管理排序

python - Django Heroku Postgres - 编程错误 : relation does not exist

python - 测试大型数据帧并根据另一个不同大小的值添加值/不合并的更有效方法

python - Pandas:删除有限的重复项