模版
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h>{
{num}}h>
<hr>
<h>{
{name}}h>
<hr>
<h>{
{people}}h>
<hr>
<h>{
{people.2.0}}h>
<hr>
<h>{
{dic}}h>
<hr>
<h>{
{dic.city}}h>
body>
html>
路由
urlpatterns = [
path('var/',views.var_test,name='var'),
]
视图
def var_test(request):
num = 12
name = "Tony"
people = ['zhangsan','lisi',['haha','lily']]
dic = {
'city':'BeiJing'}
return render(request,'var_test.html',locals())
模版
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
{# 如果该变量值为None,默认给定一个值#}
<p>{
{student|default_if_none:'no student'}}p>
<hr>
{# 如果该变量不存在,默认给定一个值 #}
<p>{
{age|default:"this value isn't exist"}}p>
<hr>
{
{dtime|date:"y-m-d H:i:s"}}
<hr>
{# 取消转义 #}
{% autoescape off %}
{
{word}}
{% endautoescape %}
<hr>
{
{join_word|join:'-'}}
body>
html>
路由
urlpatterns = [
# 过滤器
path('filter/',views.filter_test,name='filter'),
]
视图
def filter_test(request):
student = None
dtime = datetime.now()
word = "说点儿什么好呢
"
join_word = [1,2,3]
return render(request,'filter_test.html',locals())