【问题标题】:How to modularize templates in django?如何在 django 中模块化模板?
【发布时间】:2014-09-02 19:56:46
【问题描述】:

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,您可以渲染模板“base.html”,但将块“head”和“body”分别替换为模板“head.html”和“body.html”?

【问题讨论】:

  • 那你的问题是什么?

标签: python django django-templates


【解决方案1】:

如果我没听错,你需要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>

【讨论】:

    【解决方案2】:

    {% 包括 "base.html" %}

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

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2010-11-13
      相关资源
      最近更新 更多