如何在django中模块化模板?

时间:2014-09-02 19:56:47

标签: python django django-templates

base.html文件

<!DOCTYPE html>
<html>
    <head>
        {% block head %}
        {% endblock %}
    </head>
    <body>
        {% block body %}
        {% endblock %}
    </body>
</html>

head.html

<title>Example</title>

body.html

<h1>Example Django</h1>

使用django,您可以渲染模板&#34; base.html&#34;但更换块&#34;头&#34;和&#34; body&#34;分别通过模板&#34; head.html&#34;和&#34; body.html&#34;?

2 个答案:

答案 0 :(得分:3)

如果我理解你,你需要include标签 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

<!DOCTYPE html>
<html>
    <head>
        {% include "head.html" %}
    </head>
    <body>
        {% include "body.html" %}
    </body>
</html>

答案 1 :(得分:1)

{%include&#34; base.html&#34; %}

{% block head %}
 {% include "head.html" %}
{% endblock %}
{% block body %}
 {% include "body.html" %}
{% endblock %}