【问题标题】:Why won’t the images automatically size when I upload them from the Django site为什么我从 Django 站点上传图像时图像不会自动调整大小
【发布时间】:2021-11-05 08:27:36
【问题描述】:

我是新手,不懂 CSS。我在我的 Django 网站上使用了一个模板。 当我直接在 html 页面上添加图片链接时,它们会自动调整到图片网格中。 但是,当我添加代码并从网站上传图片时,它们都以不同的尺寸显示。 我希望它们都保持一致并适合预定的盒子大小。

<div class="container-fixed">
  <div class="adoption1">
  <h1 class="wow fadeIn animated">Meet our Penguins</h1>
   <div class="grid-cats wow">
{% for post in posts %}
         <div class="col-md-4 thumbs">
           <a href="">
            <figure class="effect-marley">
                <img src="{{ post.image2.url }}" alt=""/>
                    <figcaption>
                        <h2>SUPER<span>CUTE</span></h2>
                    <p>{{ post.content }}</p>
                    </figcaption>
            </figure>
            </a>
         </div>
 {% endfor %}

【问题讨论】:

    标签: html css django templates image-scaling


    【解决方案1】:

    如果您想调整图像大小,请覆盖您的模型保存方法

    from PIL import Image
    def save(self,*args,**kwargs):
        super().save(*args,**kwargs)
        img = Image.open(self.image2.path)
        if img.height>your_max_height or img.width>your_max_width:
            out_size =(your_desired_height ,your_desired_width)
            img.resize(out_size)
            img.save(self.image2.path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2011-03-09
      • 1970-01-01
      • 2011-10-24
      • 2012-07-12
      • 1970-01-01
      相关资源
      最近更新 更多