前言:Web框架的模式css
Django的模板语言html
变量python
在Django的模板语言中语言使用: {{ 变量名}}数据库
点(.) 在模板语言中一样是万能的django
{# 取l中的第一个参数 #} {{ l.0 }} {# 取字典中key的值 #} {{ d.name }} {# 取对象的name属性 #} {{ person_list.0.name }} {# .操做只能调用不带参数的方法 #} {{ person_list.0.dream }}
{% tag %}
for标签 循环的序号能够经过{{forloop}}显示bootstrap
<h3>循环取值1</h3><hr> {% for item in person_list %} <p>{{ item.name }},{{ item.age }}</p> {% endfor %} <h3>循环取值2:倒序</h3><hr> {% for item in person_list reversed %} <!--序号从1开始--> <p>{{ forloop.counter }}----->{{ item.name }},{{ item.age }}</p> <!--序号从0开始--><p>{{ forloop.counter0 }}----->{{ item.name }},{{ item.age }}</p><!-- 序号倒序 --><p>{{ forloop.revcounter }}----->{{ item.name }},{{ item.age }}</p> {% endfor %} <h3>循环取值3:字典</h3><hr> {% for k,v in d.items %} <p>{{ k }},{{ v}}</p> {% endfor %}
for循环中的一些可用参数安全
Variable | Description |
---|---|
forloop.counter |
当前循环的索引值(从1开始) |
forloop.counter0 |
当前循环的索引值(从0开始) |
forloop.revcounter |
当前循环的倒序索引值(从1开始) |
forloop.revcounter0 |
当前循环的倒序索引值(从0开始) |
forloop.first |
当前循环是否是第一次循环(布尔值) |
forloop.last |
当前循环是否是最后一次循环(布尔值) |
forloop.parentloop |
本层循环的外层循环 |
for...empty:for 标签带有一个可选的{% empty %}从句,以便在给出的组为空或者没有找到的时,添加操做网络
{% for person in person_list %} <p>{{ person.name }}</p> {% empty %} <p>sorry,no person here</p> {% endfor %}
if标签 {% if 条件 %} {{ 执行语句 }} {% endif %}app
if语句支持and,or,==,<,>, <=, >=, in , not in, is, is not判断框架
{% if i > 300 %} <p>大于{{ i }}</p> {% elif i == 200 %} <p>等于{{ i }}</p> {% else %} <p>小于{{ i }}</p> {% endif %}
with 语句 定义一个中间变量
with 上下文管理器协议
应用场景:网络链接,数据库链接,文件句柄,用到锁
{% with total=business.employees.count %} {{ total }} employee{{ total|pluralize }} {% endwith %}
{% with file_size as fs %} <h1>{{ fs }}</h1> {% endwith %}
csrf_token 这个标签用于跨站请求伪造保护
1, default: 若是一个变量是false或者为空,使用给定的默认值。不然,使用变量值
<p>default过滤器:{{ li|default:"若是显示为空,设置的解释性的内容" }}</p>
2, length:返回值的长度,对字符串和列表,字典都起做用
<h1>{{ list1|length }}</h1> <h1>{{ dic|length }}</h1> #若是 list1 是 ['a', 'b', 'c', 'd'],那么输出是 4
3, filesizeformat: 将值格式化为一个“人类可读的”文件尺寸
<h1>{{ value|filesizeformat }}</h1> #若是value是12345678 ,输出将会是117.7MB
4, date: 若是 value= datetime.now()
<h1>{{ time|date:"Y-m-d H:i:s" }}</h1> #导入模块 from datetime import datetime # time = datetime.now() #输出为 当前时间 2018-11-01 17:57:45
5, slice: 切片
<h1>{{ num|slice:"0:5" }}</h1> #num = "hello world” 输出为hello,一样遵循顾头不顾尾
6,truncatechars 截断
若是字符串子多于指定的字符数量,那么会被截断,截断的字符串将以可翻译的省略号序列(...)结尾
<h1>截断字符{{ num|truncatechars:5 }}</h1> #num = "hello world" 输出为 he... <h1>截断单词{{ num|truncatewords:5 }}</h1> #单词是以空格进行区分,num="h e l l o w o r l d" 输出为 h e l l o ...
7, safe 为了保证html在某些状况下不被转义,就使用safe。
<h1>{{ a }}</h1> # a = "<a href='https://www.baidu.com'>百度</a>" 输出为字符串 <h1>{{ a|safe }}</h1> #当连接是安全的,则是一个连接标签
8,cut 移除value中全部与给出的变量相同的字符串
<h1>{{ num|cut:"l" }}</h1> #num = "hello world" 输出为 heo word
9, join 使用字符串链接列表,与 Python中的 str.join(list)相同
<h1>{{ list3|join:"-" }}</h1> #list3 = ["h", "e", 'l', 'l', 'o'] 输出为 h-e-l-l-o
10, timesince 将日期格式设为日期起的时间,采用一个可选参数,
<h1>{{ hours|timesince }}</h1> # hours = datetime.now() - timedelta(hours=4) 输出为 4 hours
自定义filter
5, 使用自定义的filter
{% load new_filter %} <p>{{ name|new_filter }}</p> #name = "alex" 输出为 alexsb
在html中导入刚才建立的文件
重启Django项目
按照普通filter调用自定义的函数
什么状况下使用母版
使用
在母版中定义须要被替换的block
{% block page-css %} {% endblock %} {% block page-main %} {% endblock %} {% block page-js %} {% endblock %}
在子页中
先继承母版
{% extends “母版的html" %} {% block page-main %} 正文 {% endblock %}
组件
当页面上相对独立的某个部分能够单独拿出来放到一个单独的html文件中
语法
{% inclue '组件.html' %}
静态文件相关
将静态文件的路劲改为动态拼接,避免在html页面中应编码静态文件导入路劲
俩种用法
{% load static %} # 第一种 经常使用 <script src="{% static 'bootstrap-3.3.7/js/bootstrap.js' %}"></script> # 第二种 <link href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css">
自定义simpletag
自定义的inclusion_tag