简体   繁体   English

在 django(html 和 css)上将上传的项目显示到网格中

[英]display uploaded items to a grid on django(html and css)

I have a grid that has to show the uploaded images in 3 columns, first time I tried this I realized that just the first uploaded image is being shown so I added a forloop counter which dint work also.我有一个网格,必须在 3 列中显示上传的图像,第一次尝试这个时,我意识到只显示了第一个上传的图像,所以我添加了一个 forloop 计数器,它也可以工作。 What should I do to show this uploaded images on a grid.我应该怎么做才能在网格上显示这个上传的图像。

views.py视图.py

  def profile(request, username=None):
  profile, created = Profile.objects.get_or_create(user=request.user)
if username:
    post_owner = get_object_or_404(User, username=username)
    user_posts = Post.objects.filter(user_id=post_owner)

else:
    post_owner = request.user
    user_posts = Post.objects.filter(user=request.user)

args1 = {
    'post_owner': post_owner,
    'user_posts': user_posts,
}
return render(request, 'profile.html', args1)

models.py模型.py

    class Post(models.Model):
        images = models.FileField(upload_to='clips', null=True, blank=True)
        user = models.ForeignKey(User, related_name='imageuser', on_delete=models.CASCADE, default='username')

        def __str__(self):
            return str(self.text)

html html

    {% for Post in user_posts %}
      <div class="grid-wrapper">
        {% if forloop.counter|divisibleby:3 %}
          <div class="grid-item">
            {% if Post.video %}
              <video width="400" style="border-radius: 2px;">
                <source src='{{ Post.video.url }}' type='video/mp4'>
              </video>
            {% endif %}
          </div>
        {% endif %}
      </div>
    {% endfor %}

css css

    .grid-wrapper {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-auto-rows: 250px;
        grid-gap: 1rem;
        grid-auto-flow: row;
    }

    .grid-item {
        background: red;
        display: flex;
        justify-content: center;
        align-items: center;
    }

Can you try this.你能试试这个。 This will works.这将起作用。

<div class="grid-wrapper">
  {% for Post in user_posts %}
    <div class="grid-item">
      {% if Post.video %}
      <video width="400" style="border-radius: 2px;">
        <source src='{{ Post.video.url }}' type='video/mp4'>
      </video> 
      {% endif %}
    </div>
  {% endfor %}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM