{ {var}}
(var代表变量)# 插入到Students类中的方法
def get_name(self):
return self.sname
# templates 测试
def templates(request):
stu = Students.stuObj2.get(sname='刘德华')
return render(request, 'sunck/templates测试/templates1.html', {
'stu': stu})
path('template/', views.templates),
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板测试title>
head>
<body>
<h1>{
{ stu.get_name }}h1>
<h1>{
{ stu.sname }}h1>
body>
html>
{% tag %}
{% if 表达式 %}
语句
{% endif %}
{% if 表达式 %}
语句1
{% else %}
语句2
{% endif %}
{% if 表达式1 %}
语句1
{% elif 表达式2 %}
语句2
·
·
·
{% elif 表达式n %}
语句n
{% else %}
语句e
{% endif %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板测试title>
head>
<body>
<h1>{
{ stu.get_name }}h1>
<h1>{
{ stu.sname }}h1>
<h1>num={
{ num }}h1>
{% if num is 10 %}
<h2>You are a handsome boyh2>
{% elif num is 20 %}
<h2>you are a Very handsome boyh2>
{% else %}
<h2>You are not a handsome boyh2>
{% endif %}
body>
html>
在Views.py中给num传一个数字,得到效果如下图所示:
{% for 变量 in 列表 %}
{#语句1#}
{% empty %}
{#语句2#}
{% endfor %}
{ { forloop.counter }}
表示循环次数,可以用在for语句中,例如: <ul>
{% for student in stu1 %}
{#语句1#}
<li>{
{ forloop.counter }}:{
{ student.sname }}---{
{ student.scontend }}li>
{% empty %}
{#语句2#}
<li>没有学生信息li>
{% endfor %}
ul>
def templates(request):
stu1 = Students.stuObj2.all()
# stu1 = Students.stuObj2.filter(sname__startswith="刘")
# stu1 = Students.stuObj2.filter(sname__startswith="Liu")
return render(request, 'sunck/templates测试/templates1.html', {
'stu1': stu1})
{% commnet %}
被注释的内容
{% endcomment %}
{% ifequal 值1 值2 %}
语句1
{% endifequal %} # 如果值1等于值2,执行语句1,否则不执行语句1
{% include '模板目录' 参数1 参数2 %}
{% url 'namespace: name' p1 p2 %}
{% csrf_token %}
{ {var|过滤器}}
{#5. 过滤器#}
<h1>原型:{
{ str }}h1>
<h1>upper:{
{ str|upper }}h1>
<h1>lower:{
{ str|lower}}h1>
var|join:'str'
为例解释传参的过程:# templates 测试
def templates(request):
stu1 = Students.stuObj2.all()
return render(request, 'sunck/templates测试/templates1.html', {
'stu1': stu1,})
<h2>{
{ stu1|join:"--的儿子是-->" }}h2>
{ { stu1.sname|default:'啥都么的' }}
{ {dateVal|date:'y-m-d'}}
dateVal
是一个日期变量<ul>
<li>num:{
{ num }}li>
<li>num+10:{
{ num|add:10 }}li>
<li>num-5:{
{ num|add:-5 }}li>
<li>num*5:{% widthratio num 1 5 %}li>
<li>num/2:{% widthratio num 2 1 %}li>
ul>
|divisibleby:2
:可被2整除<ul>
{% for student in stu1 %}
{% if forloop.counter|divisibleby:2 %}
<li style="background-color: gray">{
{ forloop.counter }}:{
{ student.sname }}---{
{ student.scontend }}li>
{% else %}
<li style="color: orange">{
{ forloop.counter }}:{
{ student.sname }}---{
{ student.scontend }}li>
{% endif %}
{% empty %}
<li>没有学生信息li>
{% endfor %}
ul>
{ {# #}}
{%comment%}....{%endcomment%}
即使url变动,也可以根据namespace和name反向解析出正确的域名,从而实现超链接的正确执行。
<a href="{% url 'sunck:good' %}">反解析连接gooda>
# project/project/urls.py
urlpatterns = [
path('sunck/', include('sunck.urls', namespace="sunck")),
]
# project/sunck/urls.py
urlpatterns = [
path('template/good/', views.good, name='good'),
]
模板:
<a href="{% url 'sunck:good' 1 %}">反解析连接gooda>
url:
path('template/good/' , views.good, name='good'),
views视图:
# templates-反向解析 测试
def good(request, id):
return render(request, 'sunck/templates测试/good.html', {
'num': id})
good模板:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>goodtitle>
head>
<body>
<h1>good--{
{ num }}h1>
body>
html>
{% block 标签名 %}
...
{% endblock 标签名 %}
{% extends 'myApp/base.html(父模板路径)' %}
{% block 标签名 %}
内容
{% endblock 标签名 %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>basetitle>
head>
<body>
<div id="header">
<h1>headerh1>
div>
<div id="main">
{% block main %}
{% endblock main %}
div>
<div id="footer">
<h1>footerh1>
div>
body>
html>
{% extends "sunck/模板继承/base.html" %}
{% block main%}
<h1>{
{ 'main'|upper }}h1>
{% endblock main %}
# templates 测试
def templates(request):
return render(request, 'sunck/templates测试/templates1.html',
{
'str2': 'HTML转义
'
})
{#6 HTML转义#}
{
{ str2 }}
HTML转义
,未经过渲染,而我们想要的输出是{% autoescape off %}...{% endautoescape %}
关闭自动转义,将off变成on开启自动转义{#6 HTML转义#}
{
{ str2 }}
{
{ str2|escape }}
{
{ str2|safe }}
{% autoescape on %}
1{
{ str2 }}
2{
{ str2 }}
{% endautoescape %}
{% autoescape off %}
1{
{ str2 }}
2{
{ str2 }}
3{
{ str2 }}
{% endautoescape %}
某些恶意网站包含链接,表单,按钮,js,利用登录用户在浏览器中认证,从而攻击服务
MIDDLEWARE
增加'django.middleware.csrf.CsrfViewMiddleware'
{% csrf_token %}
,如:<form action="../register/" method="post">
{% csrf_token %}
姓名:<input type="text" name="name" value=""/>
<hr>
性别:<input type="radio" name="gender" value="1">男<input type="radio" name="gender" value="0">女
<hr>
年龄:<input type="text" name="age" value=""/>
<hr>
爱好:<input type="checkbox" name="hobby" value="power"/>权利<input type="checkbox" name="hobby" value="money">金钱<input type="checkbox" name="hobby" value="beauty">美女<input type="checkbox" name="hobby" value="Tesla">Tesla
<hr>
<input type="submit" value="注册">
form>
def verifi_code(request):
return render(request, 'sunck/templates测试/verificationcode.html', )
# 展示结果
def show(request):
code = request.POST.get('code')
right_code = request.session['verification_code']
if code.upper() == right_code.upper():
return HttpResponse('对了!'+right_code)
else:
return HttpResponse('错了!'+code+'----'+right_code)
path('VerificationCode/code', VerificationCode.verification_code),
path('VerificationCode/', VerificationCode.verifi_code),
path('VerificationCode/show', VerificationCode.show, name='show'),
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码title>
head>
<body>
<h3>请输入验证码h3>
<form method="post" action="{% url 'sunck:show' %}">
{% csrf_token %}
<input type="text" name="code" value=""/>
<img src="/sunck/VerificationCode/code">
<hr>
<input type="submit" value="登录"/>
form>
body>
html>