首页 技术 正文
技术 2022年11月17日
0 收藏 544 点赞 4,444 浏览 1954 个字

母板和继承

母版

  1. html页面,提取多个页面的公共部分

  2. 定义多个block块,需要让子页面进行填充

<head>
{% block page-css %} {% endblock %}
</head>
<body>

<h1>这是母板的标题</h1>

{% block page-main %} # 设置block块,可以在模板中进行个性化设置
{% endblock %}

<h1>母板底部内容</h1>
{% block page-js %}

{% endblock %}
</body>

继承

  1. 在模板的左上方写上{% extends '母板文件名' %}

  2. 重写block块 ,写在block内部

  3. 块block

     {% extends 'home.html'%}
    {% block page-main %}
    <p>《叽叽喳喳的世界》</p>
    <p>作者:叽叽喳喳</p>
    {% endblock %}  

注意

  1. {% extends ‘base.html’ %} 带上引号 不带的话会当做变量

  2. {% extends ‘base.html’ %} 上面不要写其他内容

  3. 要显示的内容写在block块中

  4. 母板定义多个block 用于定义 css 或者 js

组件

可以将常用的页面内容如导航条,页尾信息等不会已经写完不会经常更改的组件保存在单独的文件中,然后在需要使用的地方按如下语法导入即可。

 {% include '组件模板' %}

静态文件相关

静态文件引入
 引用CSS
{% load static %}
<link rel="stylesheet" href="{% static 'css/dashboard.css' %}" rel="external nofollow" >

引用JS
{% load static %}
<script src="{% static "mytest.js" %}"></script>

引用图片
{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

某个文件多处被用到可以存为一个变量
{% load static %}
{% static "images/hi.jpg" as myphoto %}
<img src="{{ myphoto }}"></img>
使用get_static_prefix
# 方式一
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />

# 方式二
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!" />
自定义simpletag

和自定义filter类似,只不过接收更灵活的参数。

定义注册simple_tag

 @register.simple_tag(name="plus")  # 可以省略(name="plus")
def plus(a, b, c):
return f"{a} + {b} + {c}"

使用自定义simple_tag

 {% load tag %}  #(load 文件名)即可

{% plus "1" "2" "abc" %}

【与filter的区别】

  • filter是过滤器,simple_tag是标签

  • filter最多传入两个参数

inclusion_tag

多用于返回html代码片段

templatetags/my_inclusion.py设计功能

 from django import template

register = template.Library()

@register.inclusion_tag('page.html') # 结合的是page.html模板
def page(num):
return {'num':range(1,num+1)} # 返回必须是一个字典

templates/page.html进行组件设计

# page模板中存放的是一个分页组件

<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li> {% for i in num %}
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ i }}</a>
</li>
{% endfor %}

<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>

templates/jjzz.html中进行调用

 <body>

{% load my_inclusion %}
{% page 5 %}

</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,129
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,601
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,444
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,218
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,852
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,940