django模板中获取列表或字典中的数据
2017-10-10 20:48
603 查看
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%}
运行项目,访问指定的路由即可显示想要显示的内容
首先我们需要将列表或者字典传到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%}
运行项目,访问指定的路由即可显示想要显示的内容
相关文章推荐
- django学习笔记---获取关联表单数据的3种方式(列表,字典,元祖)。
- django post和get方法获取数据 数据在模板上的遍历
- 无限遍历,python实现在多维嵌套字典、列表、元组的JSON中获取数据
- django post和get方法获取数据 数据在模板上的遍历
- django的模板页面里,如何获取一个集合的数据?
- 无限遍历,Python实现在多维嵌套字典、列表、元组的JSON中获取数据
- [置顶] 无限遍历,python实现在多维嵌套字典、列表、元组的JSON中获取数据
- 模板技巧之:通过弹出列表窗口获取任意数据库数据回写到模板字段
- django的html模板中获取字典的值
- 如何在Django模板中获取的session中存储的数据
- 获取Oracle 表信息 整成数据字典
- .net Response.ContentType获取返回类型及数据对应的详细列表
- 模板数据数据列表DataList模板之实例
- 织梦sitemap模板获取文章列表
- 用Django 模板处理分类汇总数据
- 数据类型之 列表、字典、元组
- 《Django》中模板接收到的几种数据类型
- 列表转换成字典数据结构
- 转载Django–模型数据的模板呈现(多对多关系增删查改)
- Django模板获取field的verbose_name