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

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%}


运行项目,访问指定的路由即可显示想要显示的内容
posted @ 2017-10-30 22:52  zhaobig  阅读(5922)  评论(0编辑  收藏  举报