简体   繁体   English

如何在我的 Django 项目之外拥有一个模板文件夹

[英]How can I have a template folder outside my Django project

I'm working on an Open Source Django app and created some design for it.我正在开发一个开源 Django 应用程序并为其创建了一些设计。 Now a customer wants to use it's own, copyrighted, design.现在,客户想要使用自己的受版权保护的设计。 After reading the Django docs I created a separate, private, GIT repository that I want to use for the new design.阅读 Django 文档后,我创建了一个单独的私有 GIT 存储库,我想将其用于新设计。 I got it almost working by adding 2 items to the settings;通过在设置中添加 2 个项目,我几乎可以正常工作; an extra entry to look for templates in a folder "funder_custom_templates" and an extra entry to look for static files in the same location.在文件夹“funder_custom_templates”中查找模板的额外条目和在同一位置查找静态文件的额外条目。 This is how I configured TEMPLATES and STATICFILES_DIR:这是我配置 TEMPLATES 和 STATICFILES_DIR 的方式:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_DIR, '..', '..', 'funder_custom_templates'),
            os.path.join(PROJECT_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'fundraiser.context_processors.cart',
            ],
        },
    },
]
STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, '..', '..', 'funder_custom_templates','static'),
    os.path.join(PROJECT_DIR, 'static'),
]

This works for overriding the base design located in the PROJECT_DIR/templates/base.html, when I create funder_customer_templates/base.html and for all the static files as expected.当我按预期创建 funder_customer_templates/base.html 和所有静态文件时,这适用于覆盖位于 PROJECT_DIR/templates/base.html 中的基本设计。 But I also want to override app specific template files like blog/templates/blog/blog_index_page.html但我也想覆盖应用程序特定的模板文件,如 blog/templates/blog/blog_index_page.html

I tried to put these files in the root of funder_custom_templates and I tried to mimic the app folders structure in funder_custom_templates but that doesn't load the app specific templates.我尝试将这些文件放在funder_custom_templates 的根目录中,并尝试模仿funder_custom_templates 中的应用程序文件夹结构,但这不会加载特定于应用程序的模板。 Is there a way to solve this?有没有办法解决这个问题?

Example project files, with the folder structure, located at: https://github.com/acidjunk/funder/tree/develop/示例项目文件,具有文件夹结构,位于: https : //github.com/acidjunk/funder/tree/develop/

Since you are using the app_directories.Loader class to load templates (specified by setting 'APP_DIRS': True, ), then for app specfic templates Django will iterate over your INSTALLED_APPS setting looking for a specific template file.由于您使用app_directories.Loader类来加载模板(通过设置'APP_DIRS': True, ),那么对于特定于应用程序的模板,Django 将遍历您的INSTALLED_APPS设置以寻找特定的模板文件。 The important thing here is that it does so in order.这里重要的是它按顺序执行。

So if you want to override blog/templates/blog/blog_index_page.html then you will need to have a custom_blog/templates/blog/blog_index_page.html inside an application that comes before blog .因此,如果您想覆盖blog/templates/blog/blog_index_page.html那么您将需要在blog之前的应用程序中有一个custom_blog/templates/blog/blog_index_page.html

I recommend wrapping up all custom static resources in their own django application and python package.我建议将所有自定义静态资源包装在它们自己的 django 应用程序和 python 包中。 This way you can simply install the package from its private repo and add it to the list of installed apps to override any static content and templates.通过这种方式,您可以简单地从其私有存储库安装该软件包,并将其添加到已安装应用程序列表中,以覆盖任何静态内容和模板。

See Django's docs for more details on template loading: https://docs.djangoproject.com/en/1.9/ref/templates/api/#django.template.loaders.app_directories.Loader有关模板加载的更多详细信息,请参阅 Django 的文档: https : //docs.djangoproject.com/en/1.9/ref/templates/api/#django.template.loaders.app_directories.Loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM