Django模板继承:母板页面与Block块详解
本文介绍Django中利用母板页面实现代码复用的核心机制。通过{% block %}定义占位符,子页使用{% extends %}继承母板并填充具体HTML内容。文章提供base.html基础模板及两个具体继承页面的代码示例,展示如何统一导航样式并差异化显示主体内容,帮助开发者构建结构清晰的Web应用界面。
Django模板继承的核心在于利用{% block xxx %}与{% endblock %}标签定义占位块。这些块通常存放在一个基础HTML文件中(如base.html),用于定义页面的通用结构或预留内容区域。
在需要复用母板结构的子页面中,首先通过{% extends "base.html" %}指令引入母板文件。随后,在子页面中重新定义{% block xxx %}...{% endblock %}块,并在其中编写具体的HTML内容,从而将子页面的特定内容填充到母板预留的位置中。
以下是base.html母板的示例代码,重点展示了块的定义方式:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="http://v3.bootcss.com/favicon.ico" rel="external nofollow" >
<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css" rel="external nofollow" >
<title>所有的书都在这里</title>
{% block page_css %}
{% endblock %}
{% block page_js %}
{% endblock %}
<!-- Bootstrap core CSS -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="http://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="external nofollow" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="http://v3.bootcss.com/examples/dashboard/dashboard.css" rel="external nofollow" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="http://v3.bootcss.com/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="http://v3.bootcss.com/assets/js/ie-emulation-modes-warning.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
{% include "nav.html" %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li class="{% block book_class %}{% endblock %}"><a href="/book_list/" rel="external nofollow" >所有的书 <span class="sr-only">(current)</span></a>
</li>
<li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/" rel="external nofollow" >出版社</a></li>
<li class="{% block author_class %}{% endblock %}"><a href="/author_list/" rel="external nofollow" >作者</a></li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
{% block main_body %}
{#这里是每个页面不同的部分#}
{% endblock %}
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="http://v3.bootcss.com/assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Just to make our placeholder images work. Don't actually copy the next line! -->
<script src="http://v3.bootcss.com/assets/js/vendor/holder.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="http://v3.bootcss.com/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
接下来是一个继承该母板的页面示例,展示了如何填充具体业务逻辑:
{# 继承母板 #}
{% extends 'base.html' %}
{% block book_class %}
active
{% endblock %}
{% block page_css %}
{% load static %}
<link rel="stylesheet" href="{% get_static_prefix %}book_list_only.css" rel="external nofollow" >
{% endblock %}
{#把自己页面的内容,填入母板里面相应的位置#}
{% block main_body %}
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">我是自定义内容,用来替换母板中指定的位置</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-8"> </div>
<div class="col-md-4"><a href="/add_book/" rel="external nofollow" rel="external nofollow" class="btn btn-primary"><i class="fa fa-plus-square"></i> 添加新书</a>
<a href="/add_publisher/" rel="external nofollow" rel="external nofollow" class="btn btn-success"><i class="fa fa-plus-square"></i> 添加出版社</a></div>
</div>
<table class="table table-dark table-hover">
<thead>
<tr>
<th>#</th>
<th>id</th>
<th>名称</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for b in books %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ b.id }}</td>
<td>{{ b.title }}</td>
<td>{{ b.publisher.name }}</td>
<td>
<a href="/del_book/?id={{ b.id }}" rel="external nofollow" class="btn btn-danger"><i class="fa fa-trash fa-fw"></i>删除</a>
<a href="/edit_book/?id={{ b.id }}" rel="external nofollow" class="btn btn-info"><i class="fa fa-pencil fa-fw"></i>编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
这是另一个继承页面的示例,演示了不同的内容填充方式:
{% extends "base.html" %}
{% block main_body %}
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">所有的出版社</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-8"> </div>
<div class="col-md-4"><a href="/add_book/" rel="external nofollow" rel="external nofollow" class="btn btn-primary"><i class="fa fa-plus-circle fa-fw"></i>添加新书</a>
<a href="/add_publisher/" rel="external nofollow" rel="external nofollow" class="btn btn-success"><i class="fa fa-plus-circle fa-fw"></i>添加出版社</a></div>
</div>
<table class="table table-dark table-hover">
<thead>
<tr>
<th>#</th>
<th>id</th>
<th>名称</th>
<th>地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for p in publisher %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ p.id }}</td>
<td>{{ p.name }}</td>
<td>{{ p.addr }}</td>
<td>
<a href="/del_publisher/?id={{ p.id }}" rel="external nofollow" class="btn btn-danger"><i class="fa fa-remove fa-fw"></i>删除</a>
<a href="/edit_publisher/?id={{ p.id }}" rel="external nofollow" class="btn btn-info"><i class="fa fa-edit fa-fw"></i>编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
{% block publisher_class %}
active
{% endblock %}
完整的练习项目代码打包为:day63_base_jb51.rar
以上内容涵盖了Django母板页面的基本使用方法,希望能为您的学习提供便利。如果您觉得有帮助,欢迎继续支持本站获取更多技术资源。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









