Django中所有应用程序的基本模板 [英] Base template for all apps in Django

查看:57
本文介绍了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 添加到该目录。

    这篇关于Django中所有应用程序的基本模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆