1 模版简介
DTL:django模板语言
核心:
变量 {{ }}
标签 {% %}
2 模版语法之变量
-变量渲染:{{变量}}
-变量深度查询:{{变量.索引/key值/方法}}
<h2>当前时间戳</h2>
@@time@@
{{ ctime }}
<h1>变量</h1>
<p>{{ age }}</p>
<p>{{ name }}</p>
<p>{{ dic }}</p>
<p>{{ li }}</p>
<h2>取出字典的名字</h2>
<p>{{ dic.name }}</p>
<h2>取出列表中的某个值</h2>
<p>{{ li.0 }}</p>
<h2>变量之对象</h2>
<p>{{ lqz }}</p>
<p>{{ lqz.test1 }}</p>
<h2>类调用方法不行,智能用对象</h2>
<p>{{ Test.test2 }}</p>
<p>{{ lqz.test2 }}</p>
def test(request): # 显示时间戳 # with open('templates/test01.html','r') as f: # data=f.read() # data=data.replace('@@time@@',str(time.time())) # return HttpResponse(data) # ctime=str(time.time()) # return render(request, 'test01.html', {'ctime': ctime}) # 变量 age=17 name='dhhfg' dic={'name':'红楼','pub_date':'2018-12-30','price':888} li=['dgfgf',18] li2=[] # locals()把所有局部变量传到模板中 class Test(): def __init__(self, name, pwd): self.name = name self.pwd = pwd def test1(self): return self.name def __str__(self): return self.pwd @classmethod def test2(cls): return 'clsss' lqz = Test('lqz', '123') # print(lqz) # print(lqz.test1()) file_size=10240000 bol=[] import _datetime today=_datetime.datetime.now() h1='hello world abc' href='<a href="http://www.baidu.com">baidu</a>' from django.utils.safestring import mark_safe href2=mark_safe(href) return render(request,'test01.html',locals())
3 模版之过滤器
变量之过滤器(是个函数)
-语法:(后面只能传一个参数)
{{变量名|过滤器的名字:参数}}
-内置过滤器:
<p>{{ age|add:3 }}</p>
<p>{{ name|length }}</p>
<p>{{ bol|default:'123' }}</p>
<p>{{ ll2|default:'我是空的' }}</p>
<p>{{ file_size|filesizeformat }}</p>
<p>{{ ctime }}</p>
<p>{{ ctime|date:'Y年m月' }}</p>
<p>{{ name|slice:'2:4' }}</p>
<p>{{ name|truncatechars:6 }}</p>
<p>{{ name2|truncatewords:2}}</p>
<p>{{ href|safe }}</p>
<h1>过滤器</h1>
<p>age+100</p>
<p>{{ age|add:100 }}</p>
<p>name lenght</p>
<p>{{ name|length }}</p>
<p>bol default</p>
<p>{{ bol|default:'123' }}</p>
<h2>filesize </h2>
<p>{{ file_size|filesizeformat }}</p>
<h2>date</h2>
<p>{{ today|date:'Y年m月d日' }}</p>
<h2>slice</h2>
<p>{{ h1|slice:':-1' }}</p>
<p>{{ h1|slice:'0:2' }}</p>
<p>{{ h1|slice:'-5:-1' }}</p>
<h2>truncatehars</h2>
<p>{{ h1|truncatechars:6 }}</p>
<h2>truncatewords</h2>
<p>{{ h1|truncatewords:2 }}</p>
<h2>href</h2>
<p>{{ href|safe }}</p>
<p>{{ href2 }}</p>
4 模版之标签
-语法:{% %}
-foo是一个可迭代对象
{% for a in foo %}
{% endfor %}
-forloop:counter,couter0,revcounter,revcouter0,first,last,parentloop:父循环的forloop对象
-for--empty的使用
{% for foo in ll2 %}
{{ foo }}
{% empty %}
没有值啊
{% endfor %}
-if判断
{% if ll2 %}
ll2 有值
{% elif ll %}
ll有值
{% else %}
ll2 没有值
{% endif %}
-with重命名
{% with dic.hobby.1 as a %}
{#{% with a=dic.hobby.1 %}#}
{{ a }}
<p>aaaa</p>
{{ a }}
{% endwith %}
<h1>模板之标签</h1>
<h2>for</h2>
{% for foo in li %}
<p>{{ foo }}</p>
{% endfor %}
<h2>li2------</h2>
{% for foo in li2 %}
<p>{{ foo }}</p>
{% empty %}
<p>no,data</p>
{% endfor %}
<h2>if</h2>
{% if li %}
data
{% elif age > 10 %}
age>10
{% else %}
age<10
{% endif %}
<h2>with</h2>
{#{% with li.1 as a %}#}
{% with a=li.0 %}
{{ a }}
{% endwith %}
5 自定义过滤器
1 确认app是否在settings中注册
2 在app下创建templatetags的模块(名字固定)
3 在模块下创建py文件(名字随意)--->在模板中{% load py文件名字%}
4 在py文件中写过滤器
from django.template import Library
register = Library()
#指定了name之后,模板上使用的时候,直接用name调用,如果没指定name,就用函数的名字
# 过滤器最多有两个参数
@register.filter(name='myadd')
def my_add(a,b):
print(a+b)
return a+b
5 模板中使用
-1 {% load py文件名字%}
-2 {{ 变量|myadd:1}}
from django.template import Library
from app01.models import *
register = Library()
#指定了name之后,模板上使用的时候,直接用name调用,如果没指定name,就用函数的名字
# 过滤器最多有两个参数
@register.filter(name='myadd')
def my_add(a,b):
print(a+b)
return a+b
{% load my_tag %}
{{ age|myadd:4 }}
{% if age|myadd:4 > 20 %}
大于20
{% endif %}
# -写一个过滤器:
# -实现过滤敏感词汇
# 操--->和谐
# 你麻痹--->民主
# -替换的字符,是可以配置的
@register.filter(name='senword')
def sen_word(word):
sen_word=Sen.objects.filter(sen=word)
if sen_word:
return sen_word[0].cha
return word
{% for i in content %}
{% load my_tag %}
<p>{{ i.data|senword }}</p>
{% endfor %}
6 自定义标签
1 确认app是否在settings中注册
2 在app下创建templatetags的模块(名字固定)
3 在模块下创建py文件(名字随意)--->在模板中{% load py文件名字%}
4 在py文件中写过滤器
from django.template import Library
register = Library()
#指定了name之后,模板上使用的时候,直接用name调用,如果没指定name,就用函数的名字
# 过滤器最多有两个参数
@register.simple_tag(name='mytag')
def my_tag(a,b,c):
return a+b+c
5 模板中使用
-1 {% load py文件名字%}
-2 {% mytag 参数1 参数2 参数3%}
# 自定义一个标签
# 标签可以传多个参数
@register.simple_tag(name='mytag')
def my_tag(a,b,c):
return a+b+c
{#标签不能跟在if判断后面#}
{#{% if mytag 1 2 3 > 30 %}#}
{# #}
{#{% endif %}#}
{% mytag 1 2 3 %}
7 标签和过滤器的区别:
1 标签可以传多个参数,过滤器最多只能传2个
2 使用过滤器{{ }} 标签使用:{% %}
3 ****重点:过滤器可以放在if判断后,标签不能放在if判断后