Django照片库 - 在画廊主页上显示单张照片

时间:2015-07-16 04:07:18

标签: django python-2.7

使用Django 1.8。我正在构建具有照片库的应用程序,其中包含属于这些画廊的照片(我的画廊被称为“喷泉”)

我的照片模型包含一个“图库”字段(布尔值为true或false),用于将照片指定为“主要喷泉照片”。在任何给定时间,只有一张照片的画廊字段为真。

试图弄清楚如何在“图库主页”中获取查询(查询),其中包含每个喷泉的一张照片的所有喷泉的列表。我的演示文稿将是喷泉名称和单张照片的响应网格。

我正在努力查看喷泉信息以及有关单张照片的信息。

我的模特:

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')

这个符号可以计算每个喷泉的照片这一事实表明它也可以获得特定的照片。所以我的观点尝试使用'aggregate'而不是'annotate':

fountain_list = Fountains.objects.filter(photos__gallery=True).order_by('name')

然而,这只是将对象中的喷泉数量减少到具有gallery field = True的喷泉数量。

但是,似乎带有gallery field = true的照片可能包含在此对象中。但我无法找回它。

我试图在模板中获取'gallery = true'照片。我假设它来自过滤后的fountain_list对象,“全部”只是一张照片。所以在模板中尝试了这个:

{% 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 %}

1 个答案:

答案 0 :(得分:2)

def home(request):
context = {}
context['photos'] = Photo.objects.first()
return render (request, 'html', context)

  photo = Photo.objects.first()
  return render(request, 'html', {'photo':photo})

HTML

<p>{{ photo.fanat.name }}</p>
<img src="{{ photo.image.url }}">
相关问题