曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
0_Python-Web-Django入门系列笔记

Python-Web: Django Template Inheritance

Templates can be reused through inheritance. Next, we first add a base.html file to the templates directory of the previous project, with the following code: !DOCTYPE html html head meta charset=utf-8 titleTitle/title /head body h1Hello World!/h1 pDA Django test./p {% block mainbody %

曦远 (DealiAxy)约 4 分钟
目录2 个章节

Templates can be reused through inheritance. Next, we first add a base.html file to the templates directory of the previous project, with the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
</head>
<body>
    <h1>Hello World!</h1>
    <p>DA Django 测试。</p>
    {% block mainbody %}
       <p>original</p>
    {% endblock %}
</body>
</html>

In the code above, the block tag named mainbody is the part that can be replaced by inheritors. All {% block %} tags tell the template engine that child templates can override these parts. hello.html inherits from base.html and replaces a specific block. The modified code of hello.html is as follows:

{% extends "base.html" %}
 
{% block mainbody %}<p>继承了 base.html 文件</p>
{% endblock %}

The first line of code indicates that hello.html inherits the base.html file. As you can see, the block tag with the same name here is used to replace the corresponding block in base.html.

About

For more interesting operations, please follow my WeChat official account: DealiAxy Every article is collected on my blog: blog.deali.cn

_EOF_

Python-Web: Django Template Inheritance

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…