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

来源:互联网 发布:数据资源共享交换平台 编辑:程序博客网 时间:2024/05/21 13:10

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


运行项目,访问指定的路由即可显示想要显示的内容