{% include %} vs {% extends %} 在 Django 模板中 [英] {% include %} vs {% extends %} in django templates

查看:23
本文介绍了{% include %} vs {% extends %} 在 Django 模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是何时扩展模板以及何时使用包含?包括用户个人资料部分(例如我们网站角落的关于我)之类的内容是否有任何用途?

When particularly extend template and when to use include ? Is include of any use with content like user profile section (like about me in the corner of our site) ?

推荐答案

扩展允许您从父模板替换块(例如内容"),而不是包括构建页面的部分(例如页眉"和页脚")).这允许您拥有一个包含完整布局的模板,并且您只需通过替换块来插入"另一个模板的内容.

Extending allows you to replace blocks (e.g. "content") from a parent template instead of including parts to build the page (e.g. "header" and "footer"). This allows you to have a single template containing your complete layout and you only "insert" the content of the other template by replacing a block.

如果在所有页面上都使用了用户配置文件,您可能希望将其放入由其他人扩展的基本模板中或将其包含在基本模板中.如果您只想在很少的页面上显示用户配置文件,您也可以将其包含在这些模板中.如果用户配置文件除了在几个页面上是相同的,请将其放在您的基本模板中的一个块中,然后可以在需要不同配置文件的那些模板中替换该块.

If the user profile is used on all pages, you'd probably want to put it in your base template which is extended by others or include it into the base template. If you wanted the user profile only on very few pages, you could also include it in those templates. If the user profile is the same except on a few pages, put it in your base template inside a block which can then be replaced in those templates which want a different profile.

这篇关于{% include %} vs {% extends %} 在 Django 模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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