博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

django template模板 母板 include导入

Posted on 2018-12-19 21:05  alex_hrg  阅读(775)  评论(0编辑  收藏  举报

 一,使用{% block name %}{% endblock %}定义一个模板,在模板页面中它的内容为空,在各页面用{% block name %}自己的标签内容{% endblock %}调用。

模板可以有多个,在各继续页面得用{% extends 'master.html' %}标明

二,{% include 'name.html' %} include导入功能可将事先写好的一些常用的页面标签,经常用的,做成一个模块那个页面用那里调用。

三,无论是母板或includer的标签,里面的模板语言{{ variable }}都能被调用它的页面所渲染

例子:

模板master.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}</title>
<style>
    .page-header{
        background-color: darkgray;
        color: aliceblue;
        height:48px;
    }
</style>
{% block css %}{% endblock %}
</head>
<body>
<div class="page-header">华为管理后台</div>
{% block content %}{% endblock %}
<script type="javascript" src="/static/jquery-3.3.1.js"></script>
{% block js %}{% endblock %}
</body>
</html>

tag.html标签:

<form>
    <input type="text"/>
    <input type="submit"/>
</form>

页面:

{% extends 'master.html' %}

{% block title %}tmp01{% endblock %}
{% block css %}
    <style>
        body{
            margin: 0 auto;
        }
    </style>
{% endblock %}
{% block content %}
    <div>
        <h1>用户管理:</h1>
        {% include 'tag.html' %}
        {% include 'tag.html' %}
        {% include 'tag.html' %}
    </div>
{% endblock %}