【发布时间】:2015-07-16 04:07:17
【问题描述】:
使用 Django 1.8。我正在构建具有照片画廊的应用程序,其中包含属于这些画廊的照片(我的画廊被称为“喷泉”)
我的照片模型包含一个“画廊”字段(布尔值 true 或 false),它将照片指定为“主喷泉照片”。在任何给定时间,只有一张照片具有此图库字段 = True。
试图弄清楚如何在“画廊主页”中获取查询(查询),该页面将列出所有喷泉,每个喷泉都有一张照片。我的演示文稿将是喷泉名称和单张照片的响应式网格。
我正在努力发送喷泉信息以及有关单张照片的信息。
我的模型:
class Fountains(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
description = models.TextField(blank=True, null=True)
street_address = models.CharField(max_length = 100, blank=True, null=True)
street_number = models.CharField(max_length = 20, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
lat = models.CharField(max_length=20, blank=True, null=True)
lon = models.CharField(max_length=20, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
#slug = models.SlugField(prepopulate_from=("name",))
modified = models.DateTimeField(auto_now=True)
#tags = TaggableManager()
#def __unicode__(self):
# return self.name.name
class Meta:
managed = True
db_table = 'fountains'
#verbose_name_plural = 'Fountains'
class Photos(models.Model):
#filename = models.CharField(max_length=100)
filename = models.ImageField(upload_to='.')
ext = models.CharField(max_length=5, blank=True, null=True)
desc = models.TextField(blank=True, null=True)
#fountain_id = models.IntegerField(blank=True, null=True)
fountain = models.ForeignKey(Fountains)
user_id = models.IntegerField(blank=True, null=True)
tag_count = models.IntegerField(blank=True, null=True)
photos_tag_count = models.IntegerField(blank=True, null=True)
comment_count = models.IntegerField(blank=True, null=True)
gallery = models.NullBooleanField(blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
#def __unicode__(self):
# return self.filename.name
class Meta:
managed = True
db_table = 'photos'
#verbose_name_plural = 'Photos'
我对主页的看法:
def home(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
#fountain_list = Fountains.objects.order_by('name')
fountain_list = Fountains.objects.filter(photos__gallery=True).order_by('name')
context_dict = { 'fountain_list': fountain_list }
return render(
request,
'app/index.html',
context_dict,
context_instance = RequestContext(request)
)
我的主页视图模板:
{% extends "app/layout.html" %}
{% block content %}
<div class="col-sm-10">
{% for fountain in fountain_list %}
<div style="float:left; class="img-responsive">
<a href="/photos/{{ fountain.id }}/"}>{{ fountain.name }}</a>
{% for photo in fountain.photos_set.all %}
<img src="{{ MEDIA_URL }}{{ photo.filename }}" alt="{{ photo.filename }}" class="thumbnail" width="100" height="100" style="border:none;"/>
</a>
{% endfor %}
</div>
{% endfor%}
</div>
{% endblock %}
我有另一个类似的观点,我使用这个来获取每个喷泉的照片数量:
fountain_list = Fountains.objects.annotate(photo_count=Count('photos')).order_by('name')
这个符号可以计算每个喷泉的照片这一事实表明它也可以得到一张特定的照片。所以我的观点是尝试使用“聚合”而不是“注释”:
fountain_list = Fountains.objects.filter(photos__gallery=True).order_by('name')
但是,这只是将对象中的喷泉数量减少到画廊字段 = True 的数量。
不过,画廊字段 = true 的照片似乎可能包含在此对象中。但我无法找回它。
我已尝试在模板中获取“gallery = true”照片。我假设它来自过滤后的喷泉列表对象,“全部”只是一张照片。所以在模板中尝试了这个:
{% for photo in fountain.photos_set.all %}
但它会检索所有喷泉的所有照片。
我通过搜索在其他地方找到了一些其他模糊相似的示例,但它们的符号完全不同(例如,'object_set' 对我不起作用)对我没有帮助。
我正在寻找一种方法,将带有 gallery = True 的一张相关照片记录添加到我的视图和模板中,以便我可以为每个喷泉显示一张照片。
我的演示文稿将是喷泉名称和单张照片的响应式网格。谢谢!
编辑以根据以下接受的答案显示新视图和模板:
我的更新视图:
def home(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
photo_list = Photos.objects.filter(gallery=True)
context_dict = { 'photo_list': photo_list }
return render(
request,
'app/index.html',
context_dict,
context_instance = RequestContext(request)
)
我更新的模板:
{% extends "app/layout.html" %}
{% block content %}
<div class="col-sm-10">
{% for photo in photo_list %}
<div style="float:left; class="img-responsive">
<a href="/photos/{{ photo.fountain.id }}/"}>{{ photo.fountain.name }}</a>
<img src="{{ MEDIA_URL }}{{ photo.filename }}" alt="{{ photo.filename }}" class="thumbnail" width="100" height="100" style="border:none;"/>
</a>
</div>
{% endfor%}
</div>
{% endblock %}
【问题讨论】:
标签: django python-2.7