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
{% for key,value in num_dict.items%}
<h1>{{ key}}:{{value}}</h1>
{% endfor%}