1、修改index页面 <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>注意:book变量的模块变量格式不要漏掉了</h1> {% for book in user %} <li>{{book}}</li> {% endfor %}
<h1>注意:获取Dict的value值</h1>
{% for book in user.values %} //items一样有效
<li>{{book}}</li>
{% endfor %}
</body> </html>
2、在views.py文件中定义dict数据 # -*- coding: cp936 -*- from django.http import HttpResponse from django.template import loader,Context def index(request): t=loader.get_template(‘index.html‘) Dict={‘age‘:25, ‘yuyan‘:‘python‘, ‘where‘:‘china‘, ‘name‘:‘xiaodeng‘, ‘sex‘:‘male‘} #向html页面传递数据 c=Context({‘user‘:Dict}) #渲染方法 t=t.render(c) return HttpResponse(t)
3、输出结果:
- age
- yuyan
- where
- name
- sex
时间: 2024-12-15 20:17:32