其实就是利用{% block xxx %}   {% endblock %}的方式定义一个块,相当于占位。存放在某个html中,比如base.html

然后在需要实现这些块的文件中,使用继承{% extends "base.html" %}的方式引入母板文件,然后在{% block xxx %}......{% endblock %}块定义中实现具体的内容。

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">
<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
<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="stylesheet"> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="http://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet"> <!-- Custom styles for this template -->
<link href="http://v3.bootcss.com/examples/dashboard/dashboard.css" 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/">所有的书 <span class="sr-only">(current)</span></a>
</li>
<li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/">出版社</a></li>
<li class="{% block author_class %}{% endblock %}"><a href="/author_list/">作者</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">
{% 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/" class="btn btn-primary"><i class="fa fa-plus-square"></i> 添加新书</a>
<a href="/add_publisher/" 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 }}" class="btn btn-danger"><i class="fa fa-trash fa-fw"></i>删除</a>
<a href="/edit_book/?id={{ b.id }}" 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/" class="btn btn-primary"><i class="fa fa-plus-circle fa-fw"></i>添加新书</a>
<a href="/add_publisher/" 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 }}" class="btn btn-danger"><i class="fa fa-remove fa-fw"></i>删除</a>
<a href="/edit_publisher/?id={{ p.id }}" 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 %}

完整的练习项目代码。

1. 母版和继承
1. 什么时候用母版?
html页面有重复的代码,把它们提取出来放到一个单独的html文件。
(比如:导航条和左侧菜单)
2. 子页面如何使用母版?
{% extends 'base.html' %} --> 必须要放在子页面的第一行

母版里面定义block(块),子页面使用block(块)去替换母版中同名的块
2. 组件
1. 什么时候用组件?
重复的代码,包装成一个独立的小html文件。
2. 如何使用?
{% include 'nav.html' %}

3. Django模板语言中关于静态文件路径的灵活写法
1. 利用Django模板语言内置的static方法帮我拼接静态文件的路径
{% load static %}
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
2. 利用内置的get_static_prefix获取静态文件路径的别名,我们自行拼接路径
{% load static %}
<link href="{% get_static_prefix %}bootstrap/css/bootstrap.min.css" rel=stylesheet>
3. as语法(一个路径多次用到,可以使用as保存到一个变量,后面就直接使用变量代替具体路径)
4. 自定义的simple_tag
比filter高级一点点
它可以接受的参数个数大于2

5. 自定义的inclusion_tag
用来返回一段html代码(示例:返回ul标签)

1. 定义阶段
在app下面新建templatetags 文件夹(注意是Python包)
新建一个py文件

from django import template
# 生成注册示例,名字必须是register
register = template.Library()
@register.inclusion_tag("ul.html")
def show_ul(num):
num = 1 if num < 1 else int(num)
data = ["第{:0>3}个数字".format(i) for i in range(1, num+1)]
return {"data": data}

2. 调用阶段
{% load xx %}
{% show_ul 10 %}

python之django母板页面的更多相关文章

  1. python Django注册页面显示头像

    python Django注册页面显示头像(views) def register(request): ''' 注册 :param request: :return: ''' if request.m ...

  2. [Python] 利用Django进行Web开发系列(一)

    1 写在前面 在没有接触互联网这个行业的时候,我就一直很好奇网站是怎么构建的.现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,但是兴趣终归还是要有的,而且是需要自己动手去实践的 ...

  3. [Python] 利用Django进行Web开发系列(二)

    1 编写第一个静态页面——Hello world页面 在上一篇博客<[Python] 利用Django进行Web开发系列(一)>中,我们创建了自己的目录mysite. Step1:创建视图 ...

  4. Python和Django在Windows上的环境搭建

    作为一个.NET程序员,真心不喜欢Python以及PHP这种松散的语法.有人说,程序员应该多学几门语言,本想学习Java,无奈感觉Java的语法太啰嗦了.很多人都推荐Python,说它的语法简洁,执行 ...

  5. Python之Django环境搭建(MAC+pycharm+Django++postgreSQL)

    Python之Django环境搭建(MAC+pycharm+Django++postgreSQL) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7828614. ...

  6. [Python] 建 Django 项目

    Python和Django的安装见这里:http://www.runoob.com/django/django-install.html 安装 Django 之后,您现在应该已经有了可用的管理工具 d ...

  7. linux下配置python环境 django创建helloworld项目

    linux下配置python环境 1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstal ...

  8. Python学习---Django下的Sql性能的测试

    安装django-debug-tools Python学习---django-debug-tools安装 性能测试: settings.py INSTALLED_APPS = [ ... 'app01 ...

  9. 【Python】Django学习一:第一个Django程序

    项目开发环境 Python 3.6 Django 1.11.5 Django安装 在开始安装Django之前,Django更新比较频繁,所以要选择合适的版本,这里选择Django1.11.5. pip ...

随机推荐

  1. 简单谈谈$.merge()

    var arr1 = [1,2,3]; var arr2 = [1,2,3]; console.log($.merge(arr1,arr2)) //[1,2,3,1,2,3],可见数组间只是合并,不会 ...

  2. cron定时任务

    1.确认系统安装了cron rpm -aq | grep crontabs 2.认识cron时间格式 3.生成定时任务 crontab -e #进入任务命令编辑模式 30 7,12,20 * * * ...

  3. firewall配置

    词解释 在具体介绍zone之前学生先给大家介绍几个相关的名词,因为如果不理解这几个名词zone就无从入手. target:目标,这个前面学生也已经给大家介绍过了,可以理解为默认行为,有四个可选值:de ...

  4. echarts常用方法(一)

    目前,项目中涉及到图表的,使用echarts的频率较多,因为UI设计一般不会考虑到是否能实现的问题,他们专注的只是显示的效果.所以作为前端开发,要对echarts进行不同程度的改造,组合,甚至重写等. ...

  5. Vue2.0使用vue-cli脚手架搭建

    一:安装node.js Node.js官网:https://nodejs.org/en/download/ 选择相应的版本即可安装 通过node自带的npm包管理工具 二.安装依赖 安装依赖:npm ...

  6. 超简DbHelper

    using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Dynamic; n ...

  7. 基于Kubernetes构建企业容器云

    前言 团队成员有DBA.运维.Python开发,由于需要跨部门向公司私有云团队申请虚拟机, 此时我在思考能否在现有已申请的虚拟机之上,再进行更加细粒度的资源隔离和划分,让本团队的成员使用, 也就是在私 ...

  8. 用html和css制作奥运五环

    <html><head><meta charset="utf-8"> <style>.circle1,.circle2,.circl ...

  9. 关于分布式代码管理工具git

    一.安装 Step1  进入官网https://www.git-scm.com/download/下载,然后安装,一直“下一步”即可 Step2  验证是否安装成功,在任意地方右键,菜单中含有Git ...

  10. Spring Boot:Web 综合开发

    Web 开发 Spring Boot Web 开发非常的简单,其中包括常用的 json 输出.filters.property.log 等 json 接口开发 在以前使用 Spring 开发项目,需要 ...