要遵照标识符规则语法: {{ var }}css
在模板中使用点语法,按顺序查询:字典查询;属性或者方法查询;数字索引查询html
若是使用的变量不存在,则插入空字符串python
在模板中调用对象的方法,可是不能给函数传参django
示例json
<body> <h1>sunck is a good man</h1> <h1>he is {{ age }} years old</h1> <h1>{{ stu.name }}--{{ stu.age }}--{{ stu.grade }}</h1> <h1>*{{ name }}*</h1> <h1>{{ stu.say }}</h1> </body>
语法:{% tag %}做用: 在输出中建立文本;控制逻辑或循环;加载外部信息到模板中使用segmentfault
ifcookie
{% if 表达式 %} 语句 {% endif %} {% if 表达式 %} 语句1 {% else %} 语句2 {% endif %} {% if 表达式1 %} 语句1 {% elif 表达式2 %} 语句2 {% elif 表达式3 %} 语句3 …… {% else %} 语句e <body> <h1>sunck is a handsome man!</h1> {% if age > 18 %} <h1>sunck is a nice man</h1> {% else %} <h1>sunck is a good man</h1> {% endif %} </body> {% endif %}
for函数
{% for 变量 in 集合 %} 语句 {% endfor %} {% for 变量 in 集合 %} 语句1 {% empty %} 语句2 # 当没有该集合或者集合为空的时候执行empty标签下的语句2 <ul> {% for stu in stus %} <li>{{ stu.name }}--{{ stu.age }}</li> {% empty %} <li>没有学生</li> {% endfor %} </ul> {% endfor %}
commentpost
做用 : 注释 单行注释({#<h1>sunck is a handsome man!</h1>#}) 多行注释
{% comment %} 注释内容 {% endcomment%} # 能够注释HTML代码和逻辑控制代码
ifequal/ifnotequal网站
做用: 判断是否相等/不相等
# 若是值1等于值2,则执行语句 {% ifequal 值1 值2 %} 语句 {% endequal %} # 若是值1不等于值2,则执行语句 {% ifnotequal 值1 值2 %} 语句 {% endequal %}
include
做用: 加载模板
格式: {% include '模板目录' %}
示例: {% include 't1.html' %}
url
做用:用于反向解析
格式: {% url 'namespace:name' 参数1 参数2 …… %}
csrf_token
做用:用于跨站请求伪造保护
格式:{% csrf_token %}
block、extends
做用:用于继承
autoescape
做用:用于HTML转义
做用: 在变量被显示以前修改它的显示
语法: {{ 变量|过滤器 }}
简单过滤器: lower upper
<h1>{{ des }}</h1> <h1>{{ des|lower }}</h1> <h1>{{ des }}</h1>
过滤器可传参,参数用引号引发来
join: <h1>{{ arr|join:'#' }}</h1>
若是一个变量没有提供或者值为False或者值为空,则可使用默认值,不然使用变量的值
default: <h1>{{ name|default:'sunck' }}</h1>
根据给定格式对一个date变量格式化显示
date : <h1>{{ value|date:'Y-m-d' }}</h1>
加减乘除
<h1>{{ age|add:2 }}</h1> <h1>{{ age|add:-2 }}</h1> {# widthratio 参数1 参数2 参数3 #} {# 参数1/参数2*参数3 #} <h1>{% widthratio age 1 2 %}</h1> <h1>{% widthratio age 2 1 %}</h1>
转义: escape;safe
自定义过滤器
过滤器就是python的函数,能够注册函数后在模板中当过滤器调用
# 在应用目录下建立名为templatetags的包 # 在templatetags目录下建立名为filters.py的文件 # -*- coding:utf-8 -*- #导入Library库 from django.template import Library #建立一个Library对象 register = Library() #定义函数 #使用装饰器注册成过滤器 @register.filter def even(value): return value % 2 == 0 @register.filter def sub(value, othre): return value - othre
# 使用自定义过滤器 {% load filters %} {{ 19|sub:9 }} {% if 19|even %} <h1>********************1</h1> {% else %} <h1>********************2</h1> {% endif %}
主路由:url(r'^', include("myApp.urls", namespace="myApp")),
子路由:url(r'^market2/$', views.market, name="market")
模板 : <a href="{% url 'myApp:market' %}">点我跳转</a>
做用: 模板继承能够减小页面内容的冲定义,实现页面内容的重用
block标签: 在父模板中预留区域,在子模板中填充
extends标签: 实现继承,写在模板文件的第一行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock title %}</title> {% block link %}{% endblock link %} {% block script %}{% endblock script %} </head> <body> <header>头</header> <div> {% block main %} {% endblock main %} </div> <footer>尾</footer> </body> </html>
{% extends 'base.html' %} {% block title %}child1{% endblock title %} {% block main %} <h1>child1</h1> {% endblock main %}
Django默认开启了HTML转义
def login(request): if request.method == "GET": # infoStr = "<h1>sunck is a good man</h1>" infoStr = "<script>alert('sunck good')</script>" return render(request, "login.html", {"infoStr":infoStr})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> {% autoescape off %} {{ infoStr }} {{ infoStr|safe }} {{ infoStr|escape }} {% endautoescape %} <form action="/login/" method="post"> 帐号:<input type="text" name="username" value=""><hr/> 密码:<input type="password" name="password" value=""><hr/> <input type="submit" value="登录"> </form> </body> </html>
跨站请求伪造保护: 某些恶意网站上包含连接、表单按钮或者JavaScript,它们会利用登录过的用户信息试图在咱们的网站上完成某些操做,这就是跨站攻击
防止CSRF
# 在settings.py中启用"'django.middleware.csrf.CsrfViewMiddleware',"中间件,工程中默认开启 # 开启保护后不管是本身仍是别人都会被屏蔽 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="http://127.0.0.1:8000/login/" method="post"> {% csrf_token %} 帐号:<input type="text" name="username" value=""><hr/> 密码:<input type="password" name="password" value=""><hr/> <input type="submit" value="登录"> </form> </body> </html>
{% csrf_token %}: 给表单页面生成一个隐藏域;写入一个名为csrftoken的cookie;给隐藏域的value设置为名为csrftoken的cookie的值;
包含工程中的css、js、img、json等文件
在工程目下建立名为static的目录用以存储静态文件
配置静态文件路径
# settings.py STATIC_URL = '/static/' STATICFILES_DIRS=[ os.path.join(BASE_DIR, "static"), ]
{#{% load static from staticfiles %}#} {% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>主页</title> {# <link rel="stylesheet" type="text/css" href="/static/css/index.css">#} <link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}"> </head> <body> <h1>sunck is a good man</h1> </body> </html>