Django中所有应用程序的基本模板

问题描述:

我有一个包含2个应用的项目

I have a project with 2 apps

project/
    blog/
        templates/
            index.html
    polls/
        templates/
            index.html
    project/
        templates/
            base.html
            index.html

现在我希望两个应用程序扩展项目 base.html 。这是要走的路吗?

Now i want that the two apps extends the projects base.html. Is this the way to go? how it is possible and are there better solutions?

已经有一个问题正在处理这个问题,但是如果您不要 >使用拆分目录:
Django项目基础模板

There is already an question which is handling this question, but it's only worth mentioning if you dont use split directories: Django project base template

tl; dr:我想使用拆分目录,并且想知道如何从多个应用程序扩展基本模板而不将其复制到每个应用程序

tl;dr: I want to use split directories and want to know how to extend a base template from multiple apps without copying it to every app

在当前Django(1.10)中 TEMPLATE_DIRS 已弃用,因此:

In current Django (1.10) TEMPLATE_DIRS is deprecated, so:


  1. 在项目根目录下添加模板目录,

  2. settings.py 中找到模板-> DIRS并像这样添加它:

  1. Add a templates directory at the project root,
  2. In settings.py find TEMPLATES -> DIRS and add it like this:

TEMPLATES = [
{
    ...
    'DIRS': [(os.path.join(BASE_DIR, 'templates')),],
    ...
}


  • base.html 添加到该目录。