1. 模板变量
注意:在manage.py路径下启动shell

My name is hjb
我们可以调用Template类中的render方法来对模板进行一个渲染,这时需要用一个Context来传递它,这个Context是一系列变量和他们值得一个集合得字典
首先建立,然后再views函数中改为,注意此处直接要传入dict对象
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader,Context
# Create your views here.
#template
def student(request):
t=loader.get_template('student.html')
return HttpResponse(t.render({
'title':'student score list'#直接传入字典
}))
然后在urls中配置一下


执行一下migrate,通过一下我们的数据库


编写html文件
由view函数向html传递字典,注意django模板的html中没有elif,所以只能嵌套✍

注意src > 60中间要留空格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<table border="1">
<tr>
<td>title</td>
{% for stu in student %}
<td>{{ stu }}</td>
{% endfor %}
</tr>
<tr>
<td>score</td>
{% for scr in score %}
<td>{{ scr }}</td>
{% endfor %}
</tr>
<tr>
<td>level</td>
{% for scr in score %}
{% if scr > 80 %}
<td> A </td>
{% else %}
{% if scr > 60 %}
<td> B </td>
{% else %}
<td> C </td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
</table>
</body>
</html>
小结:
1.模板变量 {{ var }}
2.模板语法{% if %} {% for var in list %}
- 总结一下使用django模板系统的基本规则
写模板
创建Templete对象
创建Context
调用render()方法
网友评论