Django照片库-获取单个照片以显示在库主页上
使用Django 1.8。我正在构建一个应用程序,该应用程序具有照片库,其中包含属于这些库的照片(我的库称为“喷泉”) “我的照片”模型包含一个“图库”字段(布尔值true或false),该字段将一张照片指定为“主喷泉照片”。在任何给定时间,只有一张照片具有此gallery字段=True 试图找出如何在“图库主页”中获得一个查询,其中包含所有喷泉的列表,每个喷泉有一张照片。我的介绍将是一个响应网格的喷泉名称加上单一的照片 我正在努力与视图发送喷泉信息加上关于单一照片的信息 我的模型:Django照片库-获取单个照片以显示在库主页上,django,python-2.7,Django,Python 2.7,使用Django 1.8。我正在构建一个应用程序,该应用程序具有照片库,其中包含属于这些库的照片(我的库称为“喷泉”) “我的照片”模型包含一个“图库”字段(布尔值true或false),该字段将一张照片指定为“主喷泉照片”。在任何给定时间,只有一张照片具有此gallery字段=True 试图找出如何在“图库主页”中获得一个查询,其中包含所有喷泉的列表,每个喷泉有一张照片。我的介绍将是一个响应网格的喷泉名称加上单一的照片 我正在努力与视图发送喷泉信息加上关于单一照片的信息 我的模型: class
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.filter(photos__gallery=True).order_by('name')
但是,这只是将对象中的喷泉数量减少到了gallery字段为True的喷泉数量
不过,似乎gallery字段为true的照片可能包含在此对象中。但我无法找回它
我已尝试在模板中获取“gallery=true”照片。我假设它来自过滤后的fountain_list对象,“所有”都只是一张照片。因此,在模板中尝试了以下方法:
{% for photo in fountain.photos_set.all %}
{% 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 %}
但它检索所有喷泉的所有照片
我通过搜索在其他地方找到了一些模糊相似的例子,但它们的符号完全不同(例如“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)
)
我的更新模板:
{% for photo in fountain.photos_set.all %}
{% 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 %}
{%extends“app/layout.html”%}
{%block content%}
{照片列表%中照片的百分比}
或
html
{{photo.fanat.name}
好的,太好了。我明白了,我们通过照片而不是喷泉。很高兴开始更具创造性地思考,现在我更好地理解了Django/model/query的工作方式。我修改了你的建议。我得到的是photo_list=Photos.objects.filter(gallery=True)而不是context['Photos']=photo.objects.first(),我使用photo_list而不是context['Photos'],并将其作为context_dict={'photo_list':photo_list}传递给模板。对于其他正在阅读的人,我可以在模板中得到它作为{%photo_list%}通过这种方式获取喷泉的详细信息photo.fountain.id等
photo = Photo.objects.first()
return render(request, 'html', {'photo':photo})
<p>{{ photo.fanat.name }}</p>
<img src="{{ photo.image.url }}">