Django的扩展如何工作? [英] How does Django's extends work?

查看:63
本文介绍了Django的扩展如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在/templates 目录中有我的index.html文件,在/templates/hello 中有另一个index.html.

我在/templates/hello 中创建了一个名为templates.html的文件,该文件应扩展index.html.

我可以使用其中的 {%extends index.html%} 标记来使template.html扩展两个目录中的两个index.html文件吗?

谢谢.

解决方案

您不能从多个django模板进行扩展.这是单行继承.

如果您希望/templates/index.html 作为基本索引模板,而/templates/hello/index.html 作为您好的索引模板应用程序的一部分..那么您应该/templates/hello/index.html {%extended'index.html'%} .

开头.

使用Django模板要理解的是,基本模板..被扩展的模板是THE模板..并且该模板中的所有内容都将显示,无论是在block标签之内还是之外./p>

扩展"模板时,声明的任何与扩展模板中的块匹配的块都将覆盖这些块的内容.

大多数网站/应用程序的页面之间或多或少具有一致的布局.因此,典型的模板设置是拥有一个主模板,该主模板包含页面所有各个部分的块,并带有divs和css以所需的方式排列布局.在该基本模板中放置尽可能多的常见html ..东西(通常不会从一页到下一页),并确保基本模板包含用于扩展模板时您需要填写的内容的块.这些块可以包含默认的html,如果扩展模板未覆盖该html,则会显示这些html.或者它们可以为空.

然后,对于您需要的每个新模板变体,扩展主模板并仅覆盖那些需要填充或覆盖的块.

不要将扩展视为将基本模板的代码带入对其进行扩展的模板中.Django模板不能那样工作.将基本模板视为具有页面所有基本构建块的THE模板,然后扩展名对其扩展的模板的MODIFIES进行修改.

如果您遇到另一种情况,需要在不同的模板中定义页面的各个部分,并且希望将它们组合在一起,那么您要寻找的是 {%include'templatename'%} 标记.

I have my index.html file in /templates directory and I have another index.html located in /templates/hello.

I've created a file named templates.html in /templates/hello and it should extend index.html.

Can I make template.html extends both index.html files (from both directories) using {% extends index.html %} tag in it?

Thanks.

解决方案

You cannot extend from multiple django templates.. it's a single line inheritance.

If you want /templates/index.html to be your base index template, and /templates/hello/index.html to be your index template for the hello part of your application.. then you should have /templates/hello/index.html start with {% extends 'index.html' %}.

The thing to understand with Django templates is that the base template.. the one that is 'extended', is THE template.. and everything in that template will be displayed whether it is within a block tag or outside one.

When you 'extend' a template, any blocks declared which match blocks in the template that was extended, will override the content of those blocks.

Most web sites/applications have a more or less consistent layout from page to page. So, a typical template setup would be to have a master template that contains blocks for all the various parts of the page, with divs and css to arrange the layout the way you want. Put as much as the common html.. the stuff that does not change often from one page to the next, in that base template, and make sure the base template contains blocks for anything you need to fill in when you extend the template. These blocks can contain default html which will be shown if the extending template does not override that block. Or they can be empty.

Then, for each new template variation that you need, extend the master template and override only those blocks that need to be filled in or overrriden.

Don't think of the extend as bringing the code of your base template into the template that is extending it.. Django templates do not work like that. Think of the base template as THE template which has all the basic building blocks of your page, and then the extension MODIFIES the blocks of the template that it extends.

If you have a different situation where the pieces of your page need to be defined in different templates and you wish to piece them together, then what you are looking for is the {% include 'templatename' %} tag.

这篇关于Django的扩展如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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