【问题标题】:How to serve templates for 2 different Django apps from the same folder?如何为同一文件夹中的 2 个不同 Django 应用程序提供模板?
【发布时间】:2017-02-05 08:00:48
【问题描述】:

这个问题不言自明。

基本上,我在同一个项目中运行了 2 个应用程序。我想从一个名为模板的文件夹中提供所有模板,最好放在我项目的根文件夹中。但是,Django 从应用程序的文件夹中提供模板。

我该怎么做?谢谢。

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    您可以在设置中配置TEMPLATES。 Documentation

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [], # Here, add your path to folder templates
            'APP_DIRS': True,
            'OPTIONS': {
                # ... some options here ...
            },
        },
    ]
    

    如果您的设置文件位于项目的根文件夹中。你可以这样写DIRS:

     'DIRS': [os.path.join(BASE_DIR, 'templates')],
    

    【讨论】:

      【解决方案2】:

      检查是否有线路

      django.template.loaders.app_directories
      

      在模板加载器附近的设置文件中的某个位置。如果你有的话,把它注释掉。

      如果你没有在任何地方指定 TEMPLATE_LOADERS,默认情况下 django 会加载 app_directories 加载器。 简而言之,您希望在 settings.py 中的某个位置:

      TEMPLATE_LOADERS = ['django.template.loaders.filesystem.Loader',
      # 'django.template.loaders.app_directories.Loader'
      ]
      

      然后配置您的模板,例如the documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-08
        • 1970-01-01
        • 2018-02-04
        • 1970-01-01
        • 2021-12-27
        • 2022-01-14
        • 2015-10-04
        相关资源
        最近更新 更多