63 模板语言
主要内容:模板语言https://www.cnblogs.com/maple-shaw/articles/9333821.html
1 . 变量 {{}}表示变量, 在模板渲染的时候替换成值
views里的代码:
def test(request): li = [1, 2, 3] d1 = { 'name': 'alex', 'hobby': 'study', } class Person(object): def __init__(self, name, age): self.name = name self.age = age def eat(self): return 'think eat rice' P1 = Person('lili', 22) return render(request, 'test.html', {'li':li, 'd1': d1, 'P1': P1})
html中的代码:
<body> <p>{{ li.1 }}</p> 1 <p>{{ d1.items}}</p> <p>{{ d1.name}}</p> <p>{{ P1.name}}</p> <p>{{P1.age}}</p> <p>{{P1.eat}}</p> think eat rice </body> </html>
注意: 当模板系统遇到一个.时, 会按照如下的顺序查询:
1. 在字典中查询: 如果有items关键字会优先显示关键字对应的值
2. 属性或者方法: 当类中既有属性又有方法, 会优先显示属性
2 filters: 翻译为过滤器, 用来修改变量的显示结果.
a : 语法: {{value|filter_name: 参数}}
views里的代码:
name2 = 'ALEX' file_size = 1234 num = 2 now = datetime.datetime.now() now = now - datetime.timedelta(days=7) a_html = '<a href="https://www.luffycity.com">路飞学城</a>' # a_html = '<script>alert(123)</script>' s1 = '世情薄人情恶雨送黄昏花易落' s2 = 'hello ! hello! How are you? I\'m fine! Thank you!' return render(request, 'test.html', { 'li':li, 'd1': d1, 'P1': P1, 'name':name2, 'file_size': file_size, 'num': num, 's1':s1, 'now':now, 'a_html':a_html, })
html中的代码:
<p>{{s14|default:'nothing'}}</p> {#如果没有传值的话, 就显示nothing#} <p>{{file_size|filesizeformat}}</p> {#将值转化为人类可读的文件尺寸: 如1.3k#} <p>{{ num|add:'2' }}</p> {#给原来的值加2, 现在为4#} <p>{{ name|upper }}</p> <p>{{ name|lower }}</p> {#转换成大小写#} <p>{{ s1|length }}</p> {#显示字符串的长度#} <p>{{ li|join:'-' }}</p> {#将列表拼接成字符串#} <p>{{ s1|truncatechars:6 }}</p> {#截掉的字符串将以可翻译的省略号序列(...)结尾, ...占三个字符#} <p>{{ now|date:'Y-m-d H:i:s' }}</p> {#日期格式化#} <p>{{ a_html|safe }}</p> {#为了在Django中关闭HTML的自动转义有两种方式,如果是一个单独的变量我们可以通过过滤器“|safe”的方式告诉Django这段代码是安全的不必转义。#}
3 自定义filter
1 在app01下创建一个包templatetags, 在这个包里面创建一个自定义的app01_filters.py文件
2 编写自定义的filter
from django import template # 固定写法,生成一个注册实例对象 register = template.Library() @register.filter() # 告诉Django的模板语言我现在注册一个自定义的filter def add_sb(value): """ 给任意指定的变量添加sb :param value: |左边被修饰的那个变量 :return: 修饰后的变量内容 """ return value + 'sb' @register.filter() def add_str(value, arg): return value + arg
3 使用自定义的filter
{% load app01_filters %} <p>{{ name|add_sb }}</p> <p>{{ name|add_str:'大好人' }}</p>
4 tags
a : for循环
<ul> {% for user in user_list %} <li>{{ user.name }}</li> {% endfor %} </ul>
for循环可用的一些参数
forloop.counter, 从1 开始计数 forloop.counter0, 从0 开始计数
forloop.revcounter 倒序 forloop.revcounter0
forloop.first/last 当前循环是不是第一次循环(布尔值)
forloop.parentloop 当层循环的外层循环
<table border="1"> <tbody> {% for list in li1 %} <tr> {% for name in list %} {% if forloop.counter|divisibleby:'2' and forloop.parentloop.counter|divisibleby:'2' %} 整除操作 <td style="color:red">{{ name }}</td> {% else %} <td>{{ name }}</td> {% endif %} {% endfor %} </tr> {% endfor %} </tbody> </table>
b : for...empty
<tbody>
{% for author in author_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ author.id }}</td>
<td>{{ author.name }}</td>
<td>
{% for book in author.books.all %}
{% if forloop.last %}
{{ book.title }}
{% else %}
{{ book.title }},
{% endif %}
{% empty %}
暂无出版书籍
{% endfor %}
</td>
<td>
<a href="/delete_author/?id={{ author.id }}">删除</a>
<a href="/edit_author/?id={{ author.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
c with 定义一个中间变量
{% with P1.eat as eat %} {{ eat }} {{ eat }} {% endwith %}