模板语法之变量、模版之过滤器、模版之标签、自定义标签和过滤器
# 相当于print了该变量
urls.py
url(r'^index/', views.index),
views.py
from django.shortcuts import render # Create your views here. def index(request): name='s_jun' age=20 li=[1,2,'s_jun','zone'] l2=[] t1=(1,2,3,'a','b','3') s2={'h','t','d','q','b','v',9,6,4,3,} dic={'name':'s_jun','age':20,'str2':[9,'a',6,'x',3]} def test(): print('test') return 'return test ' print(test()) # 类和对像 class Person(): def __init__(self,name,age): self.name=name self.age=age def get_name(self): return self.name @classmethod def cls_test(cls): return 'cls' @staticmethod def static_test(): return 'static' # 模板里不支持带参数 # def get_name_cs(self,ttt): # return self.name s_jun=Person('s_jun',20) zone=Person('zone',20) person_list=[s_jun,zone] person_dic={'s_jun':s_jun} # 函数不能打印内存地址,但可以把它放入到列表中,再打印内存地址(了解即可,(只打印内存地址)) # locals 会把函数当中的变量传递给'index.html'的这个网页中去。 file_size=1024 import datetime ctim=datetime.datetime.now() h2='<h2>我倒</h2>' script='<script>alert(1111111111)</script>' # # return render( request,'index.html',{'name':name}) user='s_jun' return render(request,'index.html',locals())
templates/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模板语言之变量</title> </head> <body> <p>名字:{{ name }}</p> <p>年龄(数字类型):{{ age }}</p> <p>列表:{{ li }}</p> <p>元祖:{{ t1 }}</p> <p>集合:{{ s2 }}</p> <p>字典:{{ dic }}</p> <p>函数:{{ test }}</p> <p>对像:{{ s_jun }}</p> <p>列表里套对象:{{ person_list }}</p> <p>字典里套对象:{{ person_dic }}</p> <hr> <h3>深度查询</h3> <p>列表的第0个值:{{ li.0 }}</p> <p>列表的第3个值:{{ li.3 }}</p> <p>字典取值:{{ dic.name }}</p> <p>字典取列表值:{{ dic.str2.2 }}</p> <p>对象取数据属性:{{ s_jun.name }}</p> <p>对象取绑定给对象的函数属性:{{ s_jun.get_name }}</p> <p>对象取绑定给类的函数属性:{{ s_jun.cls_test }}</p> <p>对象取静态方法:{{ s_jun.static_test }}</p> <p>把对象列表中s_jun年龄取出来:{{ person_list.1.age }}</p> {#不能调有参数的方法#} <p>字符串的方法:{{ name.upper }}</p> <h3>模板语言之过滤器</h3> {#后面就是个python中的函数,|前面的,是函数的第一个参数,冒号后面的是第二个参数#} <p>统计字符串长度:{{ name|length }}</p> <p>统计列表长度:{{ li | length }}</p> <p>过滤器之默认值:{{ l2| default:'空值' }}</p> <p>过滤器之filesizeformat--1: {{443|filesizeformat }}</p> <p>过滤器之filesizeformat--2: {{ file_size|filesizeformat }}</p> <p>过滤器之不使用date:{{ ctim }}</p> <p>过滤器之date:{{ ctim|date:'Y-m-d' }}</p> {#前闭后开区间#} <p>过滤器之slice:{{ li|slice:'2:-1' }}</p> {#支持步长#} {#<p>过滤器之slice-字符串:{{ name | slice: '0:3:3'}}</p>#} {#三个起步(显示10个,加上3个...是13个)#} <p>过滤器之truncatechars:{{ 'sdfaasdfdfsfpyyy'| truncatechars:13}}</p> {#根据空格来 显示的字段#} <p>过滤器之truncatewords:{{ '夺 jhk jh 我 kjhkl lkj 耻 囲 在 dslafj'| truncatewords:5 }}</p> <p>过滤器之不用safe:{{ h2 }}</p> <p>过滤器之用safe:{{ h2|safe }}</p> <p>过滤器之不用safe:{{ script }}</p> {#<p>过滤器之用safe:{{ script|safe }}</p>#} <p>过滤器之用add:{{ 12|add:'1' }}</p> <p>过滤器之用add:{{ 'egon'| add:'xxx' }}</p> <hr> <h3>模板语言之标签</h3> {% for bar in li %} {{ forloop }} <p>{{ forloop.first }} --->{{ forloop.counter0 }} --->{{ forloop.revcounter }}--->{{ bar }} </p> {% endfor %} <hr> {% for bar in li %} {% for i in person_list %} {# 取出外层是第几次循环#} {{ forloop.parentloop.counter }} <p> {{ forloop.first }}--->{{ forloop.counter0 }}---> {{ forloop.revcounter }}----> {{ bar }} </p> {% endfor %} {% endfor %} <hr> {# *************循环的对象是空,才会走到empty,而不是对象里面的东西为空 #} {% for bar in dic %} <p>{{ bar }}</p> {% empty %} {% endfor %} <hr> {#只循环字典的话,取到的是key值#} {% for bar in dic %} <p> {{ bar }} </p> {% endfor %} <hr> {#取到value的值#} {% for bar in dic.values %} <p>{{ bar }}</p> {% empty %} {% endfor %} <hr> {#取到key 和 value的值#} {% for k,v in dic.items %} <p>{{ k }}-----> {{ v }}</p> {% empty %} {% endfor %} <hr> {% if user %} <a href="">退出</a> {% else %} <a href="">登录</a> <a href="">注册</a> {% endif %} <hr> {#for循环判断如果是第一次,打印第一次,其他打印正常值#} {% for bar in li %} {% if forloop.first %} <p>第一次的我</p> {% elif forloop.last %} <p>最后的我</p> {% else %} <p>{{ bar }}</p> {% endif %} {% endfor %} <hr> {#with 相当于取别名,作用:变量太长,可以简化#} {% with name as ttt %} {{ ttt }} {{ name }} {{ user }} {% endwith %} <hr> {% with dic.li.2 as ttt %} {{ ttt }} {{ ttt }} {% endwith %} <hr> {#************for ,if,with 都要有结束*************#} <h3>自定义标签过滤器</h3> {% load mytag %} {#传多个参数的话:可以:'aaa:bb:cc',也可以:传列表#} <p>{{ 's_jun'| yyy:'nb' }}</p> <h5>使用自定义的标签</h5> <p>{% add_nb 's_jun' %}</p> <p>{% add_3 's_jun' 'is' 'zone' %}</p> <hr> {#过滤器,可以用在if判断中#} {% if 's_jun'|yyy:'test' %} <p></p> {% endif %} {#标签不能用在if判断(报错)#} {#{% if add_nb 's_jun' %}#} {##} {#{% endif %}#} </body> </html>