常用语法
符号
{{...}} # 变量相关 {%...%} # 逻辑相关 {#...#} # 注释
使用变量
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test(request): 2 name = '张三' 3 age = 19 4 return render(request,'test.html',{'name':name,'age':19})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 name:{{ name }} age:{{ age }} 9 </body> 10 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926144125397-1842185123.png)
for循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test(request): 2 name_list = ['张三', '李四', '王五'] 3 return render(request, 'test.html', {'list': name_list})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ list }} 9 <hr> 10 {% for name in list %} 11 {{ name }} 12 {% endfor %} 13 </body> 14 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926145150000-588477319.png)
使用字典
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test(request): 2 name_dict = {'firstname': 'zhang', 'lastname': 'san'} 3 return render(request, 'test.html', {'dict': name_dict})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ dict.firstname }} 9 {{ dict.lastname }} 10 </body> 11 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926145624246-1075730477.png)
使用对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Person: 2 def __init__(self,firstname,lastname): 3 self.firstname = firstname 4 self.lastname = lastname 5 6 def show(self): 7 return 'my firstname is {},last name is {}'.format(self.firstname,self.lastname) 8 9 def __str__(self): 10 return 'person obj name is {} {}'.format(self.firstname,self.lastname) 11 12 def test(request): 13 p = Person('zhang','san') 14 return render(request, 'test.html', {'person': p})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ person.firstname }} 9 {{ person.lastname }} 10 <hr> 11 {{ person.show }} 12 <hr> 13 {{ person }} 14 </body> 15 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926150431823-900911607.png)
Filters
语法: {{value|filter_name:params}}
default
当所修饰的变量为空时使用所指定的默认值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test(request): 2 v1= None 3 v2 = '有值' 4 return render(request, 'test.html', {'v1': v1,'v2':v2})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ v1|default:'空值' }} 9 <hr> 10 {{ v2|default:'空值' }} 11 </body> 12 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926151500730-312257333.png)
filesizeformat
文件大小格式化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ 123456789|filesizeformat }} 9 </body> 10 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926151820702-105759220.png)
slice
切片
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ 'abcdefghijklmn'|slice:'::2'}} 9 </body> 10 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926152200476-1990095671.png)
date
时间格式化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test1(request): 2 from datetime import datetime 3 now = datetime.now() 4 return render(request, 'test.html', {'now': now})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ now }} 9 <hr> 10 {{ now|date:'Y-m-d H:i:s'}} 11 </body> 12 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926152619219-30510178.png)
safe
不转义输出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def test(request): 2 a_tag = '<a href="http://www.baidu.com">超链接<a>' 3 return render(request, 'test.html', {'a_tag': a_tag})
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ a_tag }} 9 <hr> 10 {{ a_tag|safe }} 11 </body> 12 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926153041747-2055293980.png)
truncatechars
显示指定长度字符,多余部分用'...'代替
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {{ 'abcdefghijklmnopqrstuvwxyz'|truncatechars:10 }} 9 </body> 10 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926153442814-482373002.png)
自定义filter
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from django import template 2 3 register = template.Library() 4 5 6 @register.filter(name='append') 7 def add_sb(self, param1): 8 return '{}{}'.format(self, param1)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {% load myfilter %} 9 {{ '张三'| sb }} 10 </body> 11 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926154958390-1909153540.png)
Tags
自定义simpletag
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from django import template 2 3 register = template.Library() 4 5 6 @register.simple_tag(name='add_str3') 7 def my_sum(str1, str2, str3): 8 return '{}{}{}'.format(str1, str2, str3)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>test</title> 6 </head> 7 <body> 8 {% load mytag %} 9 10 {% add_str3 'a' 'b' 'c' %} 11 </body> 12 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926172736626-1438415186.png)
inclusion_tag
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from django import template 2 3 register = template.Library() 4 5 6 @register.inclusion_tag('show_info_page.html') 7 def show_info(index): 8 list = [('张三', '男'), ('李四', '女')] 9 return {'person': list[index]}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <h3>我的名字叫{{ person.0 }}</h3> 2 <p>性别{{ person.1 }}</p>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>test</title> 6 </head> 7 <body> 8 {% load mytag %} 9 {% show_info 1 %} 10 <hr> 11 {% show_info 0 %} 12 </body> 13 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926173901064-1473609369.png)
母版页
block
在母版页中定义一个块让子页面使用
{% block [block name] %}
{% endblock %}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 这是母版页的内容 9 <hr> 10 {# 定义一个block块让子页面替换 #} 11 {% block page-main %} 12 13 {% endblock %} 14 </body> 15 </html>
extends
继承母版页
{% extends '[母版页地址]' %}
{% block [block name] %}
{% endblock %}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 {% extends 'base.html' %} 2 {%block page-main %} 3 这里是子页面的内容 4 {%endblock %}
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926163052093-649962192.png)
扩展
forloop的属性
Variable | Description |
---|---|
forloop.counter |
当前循环的索引值(从1开始) |
forloop.counter0 |
当前循环的索引值(从0开始) |
forloop.revcounter |
当前循环的倒序索引值(从1开始) |
forloop.revcounter0 |
当前循环的倒序索引值(从0开始) |
forloop.first |
当前循环是不是第一次循环(布尔值) |
forloop.last |
当前循环是不是最后一次循环(布尔值) |
forloop.parentloop |
本层循环的外层循环 |
for...empty
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板测试</title> 6 </head> 7 <body> 8 {% for i in list %} 9 有值则遍历 10 {% empty %} 11 空空如也 12 {% endfor %} 13 </body> 14 </html>
with
定义一个中间变量
1 {% with total=list.count %} 2 {{ total }} 3 {% endwith %}
include
引入指定页
{% include '[页面地址]' %}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<p>in subpage.html</p>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 {% include 'subpage.html' %} 2 <hr> 3 in test.html
![](https://img2018.cnblogs.com/blog/1133411/201809/1133411-20180926164533974-293336231.png)
static&get_static_prefix
动态获取静态文件夹别名来拼接静态文件的url
配置如下
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static1"),
os.path.join(BASE_DIR, "static2"),
]
要访问项目根目录下文件夹static1里的jquery.min.js文件,原来的写法是:
<script src='/static/jquery.min.js'></script>
使用static的写法是:
<script src="{% static 'jquery.min.js'%}"></script>
使用get_static_prefix的写法是:
<script src='{% get_static_prefix %}jquery.min.js'></script>
也可给路径起一个别名:
<script src="{% static 'jquery.min.js' as jquery_path %}"></script> <!-- 之后就可以通过别名引入 --> <script src="{{ jquery_path }}"></script>