一.模板语言的变量
views.py
def index(request): #模板语言的变量 name = 'Yven' addr = False age = 180 li = [1,2,3,4,['a','b','c']] dic = { 'name':'lyf','age':18,'hobby':['run','play']} def test(): return 'test' class MyTest(): def __init__(self,name,age): self.name = name self.age = age def getName(self): return self.name @classmethod def test_cls(cls): return 'cls方法' @staticmethod def static_test(): return '静态方法' from django.utils import safestring link1 = '点我' link1 = safestring.mark_safe(link1) mytest = MyTest('Yven',18) import datetime now = datetime.datetime.now() value = [] return render(request,'index.html',locals())
html
模板语言的变量
字符串:{ { name }}
数字:{ { age }}
列表:{ { li }}
列表取值:{ { li.4.1 }}
字典取值:{ { dic.name }}
字典:{ { dic }}
函数:{ { test }}
对象:{ { mytest.age }}
对象的类方法:{ { mytest.test_cls }}
对象的静态方法:{ { mytest.static_test }}
未被解析的字符串:{ { link1 }}
二.模板的过滤器
过滤器语法:
{ { obj|filter_name:param }} 变量名|过滤器名:参数
模板的过滤器
{# default:如果该变量是False或者为空,使用给定的默认值 #}{ { addr|default:'上海' }}
{# length:返回该变量的长度 #}{ { li|length }}
{# filesizeformat:将值格式化为一个文件尺寸单位 #}{ { 1024|filesizeformat }}
{# date:将事件格式化为自定义的格式 #}{ { now|date:'Y/m/d' }}
{# slice:将值按要求切片,可设置步长 #}{ { li|slice:'1:-1' }}
{# truncatechars:字符串多于指定的字符数量,会被截断,自动添加的...也算在指定数量中 #}{ { 'abcdefghijklm'|truncatechars:8 }}
{# truncatewords:字符串词数多于指定的数量,会被阶段,自动添加...不算在指定数量中 #}{ { 'abc def ghi jkl m'|truncatewords:3 }}
{# safe:设置时,如果该值为页面可以解析的语言,就会被识别 #}{ { '点我一下'|safe }}
其他的过滤器
过滤器 | 描述 | 示例 |
---|---|---|
upper | 以大写方式输出 | { { user.name | upper }} |
add | 给value加上一个数值 | { { user.age | add:”5” }} |
addslashes | 单引号加上转义号 | |
capfirst | 第一个字母大写 | { { ‘good’| capfirst }} 返回”Good” |
center | 输出指定长度的字符串,把变量居中 | { { “abcd”| center:”50” }} |
cut | 删除指定字符串 | { { “You are not a Englishman” | cut:”not” }} |
date | 格式化日期 | |
default | 如果值不存在,则使用默认值代替 | { { value | default:”(N/A)” }} |
default_if_none | 如果值为None, 则使用默认值代替 | |
dictsort | 按某字段排序,变量必须是一个dictionary | {% for moment in moments | dictsort:”id” %} |
dictsortreversed | 按某字段倒序排序,变量必须是dictionary | |
divisibleby | 判断是否可以被数字整除 | { { 224 | divisibleby:2 }} 返回 True |
escape | 按HTML转义,比如将”<”转换为”<” | |
filesizeformat | 增加数字的可读性,转换结果为13KB,89MB,3Bytes等 | { { 1024 | filesizeformat }} 返回 1.0KB |
first | 返回列表的第1个元素,变量必须是一个列表 | |
floatformat | 转换为指定精度的小数,默认保留1位小数 | { { 3.1415926 | floatformat:3 }} 返回 3.142 四舍五入 |
get_digit | 从个位数开始截取指定位置的数字 | { { 123456 | get_digit:’1’}} |
join | 用指定分隔符连接列表 | { { [‘abc’,’45’] | join:’’ }} 返回 abc45 |
length | 返回列表中元素的个数或字符串长度 | |
length_is | 检查列表,字符串长度是否符合指定的值 | { { ‘hello’| length_is:’3’ }} |
linebreaks | 用 或 标签包裹变量 | { { “Hi\n\nDavid”|linebreaks }} 返回 Hi David |
linebreaksbr | 用标签代替换行符 | |
linenumbers | 为变量中的每一行加上行号 | |
ljust | 输出指定长度的字符串,变量左对齐 | { {‘ab’|ljust:5}}返回 ‘ab ’ |
lower | 字符串变小写 | |
make_list | 将字符串转换为列表 | |
pluralize | 根据数字确定是否输出英文复数符号 | |
random | 返回列表的随机一项 | |
removetags | 删除字符串中指定的HTML标记 | { {value | removetags: “h1 h2”}} |
rjust | 输出指定长度的字符串,变量右对齐 | |
slice | 切片操作, 返回列表 | { {[3,9,1] | slice:’:2’}} 返回 [3,9] { { 'asdikfjhihgie' | slice:':5' }} 返回 ‘asdik’ |
slugify | 在字符串中留下减号和下划线,其它符号删除,空格用减号替换 | { { '5-2=3and5 2=3' | slugify }} 返回 5-23and5-23 |
stringformat | 字符串格式化,语法同python | |
time | 返回日期的时间部分 | |
timesince | 以“到现在为止过了多长时间”显示时间变量 | 结果可能为 45days, 3 hours |
timeuntil | 以“从现在开始到时间变量”还有多长时间显示时间变量 | |
title | 每个单词首字母大写 | |
truncatewords | 将字符串转换为省略表达方式 | { { 'This is a pen' | truncatewords:2 }}返回``This is ... |
truncatewords_html | 同上,但保留其中的HTML标签 | { { '<p>This is a pen</p>' | truncatewords:2 }}返回``<p>This is ...</p> |
urlencode | 将字符串中的特殊字符转换为url兼容表达方式 | { { ‘http://www.aaa.com/foo?a=b&b=c’ | urlencode}} |
urlize | 将变量字符串中的url由纯文本变为链接 | |
wordcount | 返回变量字符串中的单词数 | |
yesno | 将布尔变量转换为字符串yes, no 或maybe | { { True | yesno }}{ { False | yesno }}{ { None | yesno }} ``返回 ``yes``no ``maybe |
三.模板的标签
标签看起来像是这样的{% tag %},标签比变量更加复杂:一些在输出中创建文本,一些通过循环或逻辑来控制流程,一些加载其后的变量将使用到的额外信息到模板中。一些标签需要开始和结束标签
for标签
和python中的for循环类似,遍历每一个元素:
{% for person in person_list%}{
{ person.name }} {% endfor %}
tips:可以利用{% for obj in list reversed %}反向完成循环
{
{forloop}}forloop.counter The current iteration of the loop (1-indexed) 当前循环的索引值(从1开始)forloop.counter0 The current iteration of the loop (0-indexed) 当前循环的索引值(从0开始)forloop.revcounter The number of iterations from the end of the loop (1-indexed) 当前循环的倒序索引值(从1开始) forloop.revcounter0 The number of iterations from the end of the loop (0-indexed) 当前循环的倒序索引值(从0开始) forloop.first True if this is the first time through the loop 当前循环是不是第一次循环(布尔值) forloop.last True if this is the last time through the loop 当前循环是不是最后一次循环(布尔值) forloop.parentloop 本层循环的外层循环
for..empty..
for标签的一个可选的从句({% empty %}),以便在给出的组是空的或者没有被找到时,可以有所操作
{% for person in person_list%}{
{ person.name }} {% empty %}no this one
{% endfor %}
if标签
同样与python中的if语句类似,会对一个变量进行判断,如果它的值为True,对应的内容块会输出.
该标签支持and、or、==、>、<、>=、<=、!=、in、not in、is、is not判断
{% if age > 90 %}老大
{% elif age > 50 and age < 90 %}老二
{% else %}小弟
{% endif %}
with标签
使用一个简单地名字缓存一个复杂的变量名,相当于取别名
{% with business.employees.count as a %} { { a }} {% endwith %}
四.自定义标签和过滤器
自定义标签和过滤器的前提:1.在settings中INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag 2.在app中创建templatetags模块(模块名只能是templatetags) 3.在templatetags中创建任意.py文件.
mytag.py文件
# 1.导入templatefrom django import template# 2.定义一个交register的变量 赋值template.Library() register = template.Library() # 3 自定义过滤器和标签 @register.filter(name='lyf') def str_add(str1, str2): return str1 + str2 @register.simple_tag() def add_nb(value): return value+'nb'
使用自定过滤器和标签
需要在使用前加{% load py文件名%}
自定义过滤器
{% load mytag %} { { 'yven'|lyf:'666' }}自定义标签
{% add_nb 'Yven' %}
注意:filter可以用在if等语句后,simple_tag不可以
{% if 'yven'|lyf:'666' %} { { 'yven'|lyf:'666' }} {% endif %}
五.模板导入和继承
模板导入
通常使用模板导入是因为页面头部页面和左侧导航菜单内容没有变化,只有右部内容在变化,如果不使用模板的导入会造成大量的代码冗余.语法:{% include "模板名称" %}
adv.html
Panel contentPanel content
index.html
首页 {% include 'adv.html' %}
模板继承
Django模板引擎中最强大也是最复杂的部分就是模板继承,模板继承可以让您创建一个基本的骨架模板,它包含站点中的全部元素,并且可以定义能够被子模板覆盖的blocks.
base.html
{% block title %} 这是母版 {% endblock %} {% block content %} 这是内容 {% endblock %}
block.html
{% extends 'base.html' %}{% block title %} 这是神奇的母板{% endblock %}{% block content %} { { block.super }} 很酷的内容{% endblock %}
上面我们定义了一个母版base.html,然后又定义了一个子模板block.html,子模板的工作是用它们的内容去填充母版中的block标签,该例子中定义了两个block.
在子模板中使用extends标签来完成继承的功能,它告诉模板引擎,这个模板继承另一个模板.如果子模板中并没有定义母版中的block,系统会使用母版中原有block的值,也可以在子模板中使用{
{ block.super }}让母版的该block值不被覆盖,与子模板中该block的值同时存在.输出之后的结果,就应该是这样的
这是神奇的母板 这是内容 很酷的内容
使用模板继承的注意事项:
-
如果你在模版中使用
{% extends %}
标签,它必须是模版中的第一个标签。其他的任何情况下,模版继承都将无法工作。 -
在base模版中设置越多的
{% block %}
标签越好。请记住,子模版不必定义全部父模版中的blocks,所以,你可以在大多数blocks中填充合理的默认内容,然后,只定义你需要的那一个。多一点钩子总比少一点好。 -
如果你发现你自己在大量的模版中复制内容,那可能意味着你应该把内容移动到父模版中的一个
{% block %}
中。 -
If you need to get the content of the block from the parent template, the
{ { block.super }}
variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using{ { block.super }}
will not be automatically escaped (see the next section), since it was already escaped, if necessary, in the parent template. -
为了更好的可读性,你也可以给你的
{% endblock %}
标签一个 名字 。例如:在大型模版中,这个方法帮你清楚的看到哪一个
{% block %}
标签被关闭了。 -
不能在一个模版中定义多个相同名字的
block
标签。
六.静态文件相关
1 写死静态文件: 2 使用 static标签函数: -{%load static%} static返回值,会拼上传参的路径 -{% static "传参"%} 如:{% static 'bootstrap-3.3.7-dist/css/bootstrap.css' %} ` 3 使用get_static_prefix 标签 -{%load static%} get_static_prefix返回值是:静态文件的地址,相当于/static/ -{% get_static_prefix %}'路径' 如:{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css