如何通过另一个模型从模板中获得价值?

时间:2019-02-09 08:33:48

标签: django django-templates

我有两个通过一对多关系连接的模型。如何从一个模型的一个领域到另一个模型来获得价值?我需要从相册模型的图像模型中获取“低”值。在我的图像模型中,“低”是图像的低分辨率副本的路径,“ path_to_image”是全尺寸图像的路径。

我使用Django 2.1,内置sqlite数据库。

models.py:

from django.db import models
from django.utils.timezone import now

class Album(models.Model):
    title = models.CharField(max_length=120)

    def __str__(self):
            return self.title

class Image(models.Model):
    path_to_image = models.CharField(max_length=120)
    low = models.CharField(max_length=120)
    pub_date = models.DateField(default=now)
    album = models.ForeignKey(Album, on_delete=models.CASCADE)

    def __str__(self):
            return self.path_to_image

views.py:

def albums(request):
    albums_list = Album.objects.all()
    paginator = Paginator(albums_list, 20)
    page = request.GET.get('page')
    albums = paginator.get_page(page)
    random_idx = random.randint(0, Image.objects.count() - 1)
    image = Image.objects.all()[random_idx]

    context = {
            'albums': albums,
            'image': image,
    }

    return render(request, 'gallery/albums.html', context)

albums.html:

<div class="item2 my-gallery responsive" itemscope itemtype="http://schema.org/ImageGallery">
    {% for album in albums %}
       <div class="gallery">
            <a href="/albums/{{ album.id }}">
                <img src="{{album.image_set.model.low}}" width="300" height="225">
            </a>
            <div class="desc">{{album}}</div>
        </div>
    {% endfor %}
</div>

我想从<img>标签的图片模型中获取价值。因此,是否有可能不完全重构所有代码? (此外,在views.py中,我尝试从相册中获取随机图像以将其设置为预览,但它也不起作用。)

2 个答案:

答案 0 :(得分:1)

要获取每个相册中第一张图像的low值,您可以执行以下操作:

albums.html:

<div class="item2 my-gallery responsive" itemscope itemtype="http://schema.org/ImageGallery">
    {% for album in albums %}
       <div class="gallery">
            <a href="/albums/{{ album.id }}">
                <img src="{{album.image_set.first.low}}" width="300" height="225">
            </a>
            <div class="desc">{{album}}</div>
        </div>
    {% endfor %}
</div>

唯一的区别是{{album.image_set.first.low}},我们从中选择集合中的第一项。

答案 1 :(得分:0)

据我了解,您当前的视图会返回随机图像,该图像可能根本与相册无关。我会遍历这些相册,并在每个相关的相册中查找一个随机图像。像这样:

def albums(request):
albums = Album.objects.all()
albums_list = []
for album in albums:
    album_list.append({
        album_images = Image.objects.filter(album=album)
        'id'=album.pk,
        'image_low'= album_images[random.randint(0, album_images.count() - 1)].low
        'title'=album.title
    })
paginator = Paginator(albums_list, 20)
page = request.GET.get('page')
albums = paginator.get_page(page)

context = {
        'albums': albums,
        'image': image,
}

return render(request, 'gallery/albums.html', context)

然后您可以像这样访问模板中的相册

    <div class="item2 my-gallery responsive" itemscope itemtype="http://schema.org/ImageGallery">
    {% for album in albums %}
       <div class="gallery">
            <a href="/albums/{{ album.id }}">
                <img src="{{album.image_low}}" width="300" height="225">
            </a>
            <div class="desc">{{album.title}}</div>
        </div>
    {% endfor %}
</div>