【问题标题】:how can I display images in carousel on the post如何在帖子的轮播中显示图像
【发布时间】:2021-02-23 05:51:18
【问题描述】:

我创建了一个帖子,允许我上传多个图像,帖子包含标题、描述和图像,但我试图在这个模板中显示,但在每个帖子上都显示了所有照片,不仅是已上传的照片对于那个帖子

这是我的模板代码

{% for post in posts %}
<section class="container" style="margin-top: -0.8rem;">
    <div class="carousel-inner">
      <div style="background-color: #000000" class="card text-center">
        <div class="card-body">
          <h1 class="card-title" style="color: #FFFFFF; letter-spacing: 6px; font-size: 20px; margin-bottom: -0.7rem;">{{ post.title }}</h1>
        </div>
      </div>

      <div id="{{ post.id }}" class="carousel slide" data-interval="false" data-ride="carousel">
        <ol class="carousel-indicators">
          {% for p in photos %}
          <li data-target="#carouselExampleIndicators" data-slide-to="{{forloop.counter0}}" class="{% if forloop.counter0 == 0 %} active {% endif %}"></li>
          {% endfor %}
        </ol>
        <div class="carousel-inner" role="listbox">
          {% for p in photos %}
          <div class="carousel-item {% if forloop.counter0 == 0 %} active {% endif %}" style="background-color: #000000">
            <img class="d-block w-100" src="{{ p.image.url }}" alt="First slide">
          </div>
          {% endfor %}
        </div>
        <a class="carousel-control-prev" href="#{{ post.id }}" role="button" data-slide="prev">
          <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#{{ post.id }}" role="button" data-slide="next">
          <span class="carousel-control-next-icon" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
      <div style="background-color: #000000" class="card text-center">
        <div class="card-body">
          <p class="container" style="color: #FFFFFF; font-size: 10px; letter-spacing: 2px; text-align: justify; margin-top: -1rem;">{{post.description}}</p>
        </div>
      </div>
    </div>
</section>
{% endfor %}

这里是模型代码-----------------

class Photo(models.Model):
title = models.CharField(default='', max_length=200)
description = models.TextField()

def __str__(self):
    return self.title

class PhotoImage(models.Model):
    post = models.ForeignKey(Photo, default=None, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='images/')

    def __str__(self):
        return self.post.title
    

这是查看代码 ----------------------------------

class PhotographyListView(ListView):
    model = Photo
    model = PhotoImage
    template_name = 'photography.html'
    ordering = ['-id']

    def get_context_data(self, **kwargs):
        context = super(PhotographyListView, self).get_context_data(**kwargs)
        context['posts'] = Photo.objects.order_by('-id')
        context['photos'] = PhotoImage.objects.filter()
        return context

【问题讨论】:

    标签: python python-3.x django django-models django-templates


    【解决方案1】:

    在模板中,仅循环显示附加到帖子的照片。

    {% for p in post.photoimage_set.all %}
       <li data-target="#carouselExampleIndicators" data-slide-to="{{forloop.counter0}}" class="{% if forloop.counter0 == 0 %} active {% endif %}"></li>
    {% endfor %}
    

    不再需要 ListView 中的 PhotoImage 查询。

    • 为了减少额外的查询,您可能需要查找在Photo.objects.order_by('-id') 查询中添加prefetch_related

    【讨论】:

    • 你能给我一个我的代码的例子吗,因为我试过了,我不知道怎么做它不起作用
    • 我更新了答案以允许代码格式化。您可以使用 reverse_foreignkey 查找吗?也就是说,获取所有附加到帖子的照片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2017-11-20
    • 2020-01-02
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多