【发布时间】:2013-03-29 13:10:54
【问题描述】:
我正在尝试呈现使用zip() 压缩的列表列表。
list_of_list = zip(location,rating,images)
我想将此list_of_list 渲染为模板,并且只想显示每个位置的第一张图片。
我的位置和图像模型如下:
class Location(models.Model):
locationname = models.CharField
class Image(models.Model):
of_location = ForeignKey(Location,related_name="locs_image")
img = models.ImageField(upload_to=".",default='')
这是压缩列表。如何仅访问模板中每个位置的第一张图片?
【问题讨论】:
-
只是好奇,你为什么要压缩?为什么不使用关系?
-
@Bibhas,你的意思是:
all_locations = Location.objects.all()和模板中的{% for loc in all_locations %} {% for img in loc.locs_image.all %}{{img.img.name}} {% endfor %} {% endfor %}? -
是的。为什么不呢?如果你只想要一个项目,你可以在模板中使用
slice标签。
标签: python django django-templates