1、组成:HTML+逻辑控制代码
变量(使用双大括号来引用变量)
语法格式:{{ }}
两个对象:Template和Context对象from django.template import Context,Template import datetime
def current_time(request): now = datetime.datetime.now() t = Template('<html><body>现在时刻是:<h1 style="color:red">{{current_date}}</h1></body></html>') c = Context({'current_date':now}) html=t.render(c) print html return HttpResponse(html)2、Template 深度变量查找(万能句点符) list dic class time
class Person(object): def __init__(self,name): self.name = name
def index(request): list = ['pk','ck'] dic = {'a':123,'b':456} p = Person('天天') t = datetime.date(1999,9,10)
html
list:{{ list.1 }}<hr> dic:{{ dic.a }}<hr> class:{{ p.name }}<hr> time:{{ t.year }}-{{ t.month }}-{{ t.day }}3、Template 过滤器,语法格式:{{obj | filter:param}}
add 给变量加上相应的指,是数字则相加,是字符串则拼接
capfirst 使字符串首字母大写
cut 从字符串中移除指定内容
date 格式化日期
default 给变量设置默认值,如果后端未传过来值或传过来空值,则在前端页面显示设置的默认值
number:{{ number | add:10 }}<hr> str:{{ str | add:'efg' }}<hr> str:{{ str | capfirst }}<hr> str2:{{ str2 | cut:' ' }}<hr> time:{{ t | date:'Y-m-d'}}<hr> default:{{ kong | default:'default' }}