模板配置
1.创建模板文件夹:<项目名》/templates
2.在settings.py中TEMPLATES配置项
BACKEND:指定模板的引擎
DIRS:模板的搜索目录(可以是一个或多个)
APP_DIRS:是否要在应用中templates文件夹中搜索模板文件
OPTIONS:有关模板选项
3.配置项中需要徐工的部分
设置DIR-'DIRS':[os.path.join(BASE_DIR,'templates')],
模板的加载方式
使用render()直接加载并响应模板
在视图函数中:
from django.shortcuts import render
return render(request,'模板文件名‘,字典数据)
视图层与模板层之间的交互
1.视图函数中可以将python变量封装到字典中传递到模板
样例
例如:
def xxx_view(request)
dic = {'变量1’:‘值1’,
‘变量2’:‘值2’,
}
return render(request,'xxx_html',dic)
2.模板中,我们可以用{{变量名}}的语法调用视图传进来的变量
例如:
<h3>{{username}}{{age}}这是模板层测试<h3>
模板的变量
能传递到模板中的数据类型
str - 字符串 int - 整型
list - 数组 tuple - 元组
dic - 字典 func - 方法
obj - 类实例化的对象
def test_html_param(request):
dic={}
dic['int'] = 88
dic['str'] = 'guoxiaonao'
dic['lst'] = ['tom','jack','lily']
dic['func' = say_hi
dic['class_obj'] = dog()
return render(request,'test_html_param.html',dic)
def say_hi():
return'hahaha'
class Dog:
def say(self):
return'wangwang'
在模板中使用变量语法
- {{变量名}}
- {{变量名.index}} #index是索引
- {{变量名.key}}
- {{对象.方法}}
- {{函数名}}
模板标签作用:
将一些服务器端的功能切入到模板中,例如流程控制等
标签语法
{{% 标签 %}}
......
{{% 结束标签 %}}
if标签
语法:
{% if 条件表达式1 %}
...
{% elif 条件表达式2 %}
...
{% else %}
....
{% end if %}
注意:
1.if 条件表达式里可以用的运算符==,!=,<,>,<=,>=,in,not in,is,is not not,and,or
2.在if 标记中使用实际括号是无效的语法。如果您需要他们指示优先级,则应该使用嵌套的i标记
官方文档https://docs.djangoproject.com/zh-hans/2.2/ref/templates/builtins/#if
简单编写计算器实操:
views.py中:
def mycal(request):
if request.method=='GET':
return render(request,'mycal.html')
elif request.method=='POST':
x = int(request.POST['x'])
y = int(request.POST['y'])
op = request.POST['op']
result = 0
if op == 'add':
result = x + y
elif op == 'sub':
result= x - y
elif op == 'mul':
result = x * y
elif op =='div':
result=x / y
return render(request,'mycal.html',locals())
mycal.html中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算器</title>
</head>
<body>
<form action='/mycal' method = 'post'>
<input type='text' name="x" value="{{ x }}">
<select name="op">
<option value="add" {% if op == 'add' %}selected{% endif %} > +加</option>
<option value="sub" {% if op == 'sub' %}selected{% endif %}> -减</option>
<option value="mul" {% if op == 'mul' %}selected{% endif %} > *乘</option>
<option value="div" {% if op == 'div' %}selected{% endif %} > /除</option>
</select>
<input type='text' name="y" value="{{ y }}"> =
<span>{{ result }}</span>
<div><input type="submit" value='开始计算'></div>
</form>
</body>
</html>
for 标签
语法:
{% for 变量 in 可迭代对象 %}
.....循环语句
{% empty %}
.....可迭代对象无数据时填充语句
{% endfor %}
官方文档:官方文档https://docs.djangoproject.com/zh-hans/2.2/ref/templates/builtins/#for
for标签内置变量-forloop
变量 | 描述 |
forloop.counter | 循环的当前迭代(从1开始索引) |
forloop.counter() | 循环的当前迭代(从0开始索引) |
forloop.revcounter | counter值得倒序 |
forloop.revcounter0 | 从0开始counter值得倒序 |
forloop.first | 如果这是第一次通过循环,则为真 |
forloop.lst | 如果这是最后一次循环,则为真 |
forloop.parentloop | 当嵌套循环,parentloo p表示外层循环 |
使用方式:
{%for name in lst %}
{% if forloop.first %} ````````{% endif %}
<p>{{forloop.counter}}{{name}}</p>
{% if forloop.last %} ///{% endif %}
{% empty %}
当前没有数据
{% endfor %}