Django模板之基本的 for 循环 和 List内容的显示方式
作者:时光足迹 时间:2021-09-24 05:18:24
工程结构
views.py
def home(request):
TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
return render(request, 'home.html', {'TutorialList': TutorialList})
url.py
home2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基本的 for 循环 和 List内容的显示</title>
</head>
<body>
<h1>教程列表:</h1>
{% for i in TutorialList %}
<h1>{{ i }}</h1>
{% endfor %}
</body>
</html>
效果展示
补充知识:django获取某一个字段的列表,values/values_list/flat
如下所示:
class Building(models.Model):
corporation = models.ForeignKey('company.Corporation', verbose_name=u'学校', related_name='buildings')
number = models.CharField(u'楼栋编号', max_length=10, unique=True, db_index=True)
create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
获取Building的number字段列表
In [1]: from apps.dormitory.models import Building
In [2]: buildings = Building.objects.values('number')
In [3]: buildings
Out[3]: [{'number': u'1'}, {'number': u'2'}, {'number': u'3'}, {'number': u'4'}, {'number': u'5'}]
In [4]: buildings_ = Building.objects.values_list('number')
In [5]: buildings_
Out[5]: [(u'1',), (u'2',), (u'3',), (u'4',), (u'5',)]
In [6]: buildings_list = Building.objects.values_list('number', flat=True)
In [7]: buildings_list
Out[7]: [u'1', u'2', u'3', u'4', u'5']
从以上代码可以看出:
values方法可以获取number字段的字典列表。
values_list可以获取number的元组列表。
values_list方法加个参数flat=True可以获取number的值列表。
来源:https://blog.csdn.net/gaoxiaoba/article/details/52469530
标签:Django,for,循环,List
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
登陆成功后自动计算秒数执行跳转
2023-10-10 19:47:59
![](https://img.aspxhome.com/file/2023/8/56348_0s.gif)
Python科学画图代码分享
2023-08-19 07:06:25
![](https://img.aspxhome.com/file/2023/2/65532_0s.jpg)
pytorch加载自己的图片数据集的2种方法详解
2023-08-09 00:35:48
![](https://img.aspxhome.com/file/2023/5/63225_0s.png)
瞬间的设计 I
2009-12-25 18:54:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/untitled-11-14s.png)
再谈float菜单局中
2009-12-21 19:57:00
Msxml2.XMLHTTP Microsoft.XMLHTTP new XMLHttpRequest用法
2010-03-30 09:43:00
SQL Server 日期相关资料详细介绍
2012-07-11 16:14:07
合理的网页设计具有哪些特征
2007-10-09 13:24:00
![](https://img.aspxhome.com/file/UploadPic/200710/9/2007109133342457s.jpg)
Go语言的Channel遍历方法详解
2023-09-24 07:50:08
![](https://img.aspxhome.com/file/2023/4/101054_0s.png)
教你快速掌握SQL语言中游标的使用技巧
2009-01-08 16:24:00
asp采集常用的几个FUCTION
2007-09-05 19:45:00
Go语言实现文件上传
2023-07-08 18:26:38
Tensorflow 实现线性回归模型的示例代码
2023-07-03 09:00:41
![](https://img.aspxhome.com/file/2023/6/61166_0s.jpg)
python中for语句简单遍历数据的方法
2023-04-18 11:26:45
FrontPage XP设计教程2——网页的编辑
2008-10-11 12:16:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017121938712s.jpg)
CSS sprites图片拼合生成器
2007-10-15 12:25:00
空行不空格式排版组织原理
2010-05-03 14:46:00
![](https://img.aspxhome.com/file/UploadPic/20105/3/20105314576317s.jpg)
PHP扩展之kafka安装应用案例详解
2023-09-06 09:53:43
分享十款最出色的PHP安全开发库中文详细介绍
2023-06-15 01:04:59
![](https://img.aspxhome.com/file/2023/1/55491_0s.jpg)
Django models.py应用实现过程详解
2021-07-18 11:11:08
![](https://img.aspxhome.com/file/2023/1/70181_0s.png)