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