views.pycss
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 = '<a href="https://www.baidu.com">点我<a>' link1 = safestring.mark_safe(link1) mytest = MyTest('Yven',18) import datetime now = datetime.datetime.now() value = [] return render(request,'index.html',locals())
htmlhtml
<h2>模板语言的变量</h2> <div> <p>字符串:{{ name }}</p> <p>数字:{{ age }}</p> <p>列表:{{ li }}</p> <p>列表取值:{{ li.4.1 }}</p> <p>字典取值:{{ dic.name }}</p> <p>字典:{{ dic }}</p> <p>函数:{{ test }}</p> <p>对象:{{ mytest.age }}</p> <p>对象的类方法:{{ mytest.test_cls }}</p> <p>对象的静态方法:{{ mytest.static_test }}</p> <p>未被解析的字符串:{{ link1 }}</p> </div>
过滤器语法:python
{{ obj|filter_name:param }} 变量名|过滤器名:参数
<h2>模板的过滤器</h2> <div> {# default:若是该变量是False或者为空,使用给定的默认值 #} <p>{{ addr|default:'上海' }}</p> {# length:返回该变量的长度 #} <p>{{ li|length }}</p> {# filesizeformat:将值格式化为一个文件尺寸单位 #} <p>{{ 1024|filesizeformat }}</p> {# date:将事件格式化为自定义的格式 #} <p>{{ now|date:'Y/m/d' }}</p> {# slice:将值按要求切片,可设置步长 #} <p>{{ li|slice:'1:-1' }}</p> {# truncatechars:字符串多于指定的字符数量,会被截断,自动添加的...也算在指定数量中 #} <p>{{ 'abcdefghijklm'|truncatechars:8 }}</p> {# truncatewords:字符串词数多于指定的数量,会被阶段,自动添加...不算在指定数量中 #} <p>{{ 'abc def ghi jkl m'|truncatewords:3 }}</p> {# safe:设置时,若是该值为页面能够解析的语言,就会被识别 #} <p>{{ '<a href="#">点我一下</a>'|safe }}</p> </div>
其余的过滤器git
过滤器 | 描述 | 示例 |
---|---|---|
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 }} 返回 Hibootstrap 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标签app
和python中的for循环相似,遍历每个元素:dom
{% for person in person_list%} <p>{{ person.name }}</p> {% 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%} <p>{{ person.name }}</p> {% empty %} <p>no this one</p> {% endfor %}
if标签
一样与python中的if语句相似,会对一个变量进行判断,若是它的值为True,对应的内容块会输出.
该标签支持and、or、==、>、<、>=、<=、!=、in、not in、is、is not判断
<div> {% if age > 90 %} <p>老大</p> {% elif age > 50 and age < 90 %} <p>老二</p> {% else %} <p>小弟</p> {% endif %} </div>
with标签
使用一个简单地名字缓存一个复杂的变量名,至关于取别名
<div> {% with business.employees.count as a %} {{ a }} {% endwith %} </div>
自定义标签和过滤器的前提: 1.在settings中INSTALLED_APPS配置当前app,否则django没法找到自定义的simple_tag 2.在app中建立templatetags模块(模块名只能是templatetags) 3.在templatetags中建立任意.py文件.
mytag.py文件
# 1.导入template from 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文件名%}
<h1>自定义过滤器</h1> {% load mytag %} {{ 'yven'|lyf:'666' }} <h1>自定义标签</h1> {% add_nb 'Yven' %}
注意:filter能够用在if等语句后,simple_tag不能够
{% if 'yven'|lyf:'666' %} {{ 'yven'|lyf:'666' }} {% endif %}
一般使用模板导入是由于页面头部页面和左侧导航菜单内容没有变化,只有右部内容在变化,若是不使用模板的导入会形成大量的代码冗余. 语法:{% include "模板名称" %}
adv.html
<div class="adv"> <div class="panel panel-warning"> <div class="panel-body"> Panel content </div> <div class="panel-footer">Panel footer</div> </div> <div class="panel panel-danger"> <div class="panel-body"> Panel content </div> <div class="panel-footer">Panel footer</div> </div> </div>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <style> .head{ height:100px; background-color: #9d9d9d; } </style> </head> <body> <div class="head"></div> <div class="container"> <div class="row"> <div class="col-md-3"> {% include 'adv.html' %} </div> <div class="col-md-9"> </div> </div> </div> </body> </html>
Django模板引擎中最强大也是最复杂的部分就是模板继承,模板继承可让您建立一个基本的骨架模板,它包含站点中的所有元素,而且能够定义可以被子模板覆盖的blocks.
base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %} 这是母版 {% endblock %}</title> </head> <body> <div class="head"> {% block content %} 这是内容 {% endblock %} </div> </body> </html>
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的值同时存在.
输出以后的结果,就应该是这样的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 这是神奇的母板 </title> </head> <body> <div class="head"> 这是内容 很酷的内容 </div> </body> </html>
使用模板继承的注意事项:
若是你在模版中使用 {% 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 写死静态文件:<link rel="stylesheet" href="/static/css/mycss.css"> 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