您的位置:首页 > 编程语言 > Go语言

django模板中获取列表或字典中的数据

2017-10-10 20:48 603 查看
django模板中获取列表或字典中的数据

首先我们需要将列表或者字典传到html模板中

该操作在app>views.py文件中完成。

在views.py文件中的return render()中携带list列表,或者dict字典

举例:

views.py

return render(request,'index.html',list)

这样将list列表传递到index.html文件中

然后在index.html文件中操作,遍历出列表中的每一个数据

index.html

{% for num in num_list%}
<h1>{{ num }}</h1>
{% endfor%}

获取字典中的key和value

views.py

return render(request,'index.html',dict)

index.html

{% for key,value in num_dict.items%}
<h1>{{ key}}:{{value}}</h1>
{% endfor%}

运行项目,访问指定的路由即可显示想要显示的内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: