{% include %} vs {% extends %} in django templates

2019-01-11 09:37发布

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) ?

2条回答
Emotional °昔
2楼-- · 2019-01-11 10:09

See about django template inheretance.

Extends sort of 'includes' the parent template and then can overwrite parts of it for different functionality.

Include does a simple include rendering a template in a current context.

查看更多
仙女界的扛把子
3楼-- · 2019-01-11 10:11

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.

查看更多
登录 后发表回答