如何在ListView中获取ManyToMany字段对象

时间:2019-10-10 09:01:39

标签: python django django-models django-templates django-views

我正在尝试使用ListView列出我的视频。我有两个表,一个叫“视频”,另一个叫“缩略图”。我已经成功列出了我的视频,但无法列出缩略图。 这是我的代码:

我的模特:

class Thumbnails(models.Model):

    src = models.URLField()

    class Meta:
        verbose_name = "Thumb"
        verbose_name_plural = "Thumbs"

    def __str__(self):
        return self.src

class Videos(models.Model):

    title = models.CharField(max_length=200)
    duration = models.CharField(max_length=10)
    views = models.IntegerField()
    rating = models.CharField(max_length=10)
    video_id = models.CharField(max_length=25)
    slug = models.SlugField(unique=True)
    tags = TaggableManager()
    thumbnails = models.ManyToManyField(Thumbnails,null=True,blank=True)

    class Meta:
        verbose_name = "Video"
        verbose_name_plural = "Videos"

    def __str__(self):
        return self.title

我的观点:

class HomePage(ListView):

    model = Videos
    template_name = "home/main.html"
    ordering = ['-pk']

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

我的模板:

  {% for obj in object_list %}
                <li>
                    <div class="thumb">
                        <div class="thumb-content">
                            <a href="#">
{% for thum in obj.thumbnail %}
                                <img src="{{ thum }}">
{% endfor %}
                                <span class="duration"><i class="fa fa-clock-o"></i> {{ obj.duration }}</span>
                            </a>
                        </div>
                        <div class="thumb-header" style="height: 45px;">
                            <h3>{{ obj.title }}</h3>
                        </div>
                        <div class="thumb-footer">
                            <span class="views"><i class="fa fa-eye"></i> {{ obj.views }}</span>
            <span class="like"><i class="fa fa-thumbs-o-up"></i> {{ obj.rating|floatformat:2  }}%</span>
                        </div>
                    </div>
                </li>
                {% endfor %}

输出如下:

<li>
                    <div class="thumb">
                        <div class="thumb-content">
                            <a href="#">

                                <span class="duration"><i class="fa fa-clock-o"></i> 11:00</span>
                            </a>
                        </div>
                        <div class="thumb-header" style="height: 45px;">
                            <h3>Beautiful girl wants to be a famous model</h3>
                        </div>
                        <div class="thumb-footer">
                            <span class="views"><i class="fa fa-eye"></i> 2216</span>
            <span class="like"><i class="fa fa-thumbs-o-up"></i> 90,77%</span>
                        </div>
                    </div>
                </li>

我想显示缩略图,但什么都没有显示。有什么问题吗?

1 个答案:

答案 0 :(得分:1)

该字段称为thumbnails,而不是thumbnail。并且您需要调用其all方法:

{% for thum in obj.thumbnails.all %}