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


運行項目,訪問指定的路由即可顯示想要顯示的內容


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM