如何在Django中添加适当的图像尺寸宽度和高度?

huangapple go评论57阅读模式
英文:

How to add appropriate image size width and height in Django?

问题

article/models.py

class article(models.model):
    image1 = models.ImageField(upload_to='images', blank=True)

code head.html

<meta property="og:image" content="{{ article.image1.url }}" />
<meta property="og:image:secure_url" content="{{ article.image1.url }}" />
<meta property="og:image:width" content="{{ article.image1.width }}" />
<meta property="og:image:height" content="{{ article.image1.height }}" />

添加尺寸宽度和高度生成。

英文:

I have models for image and upload image, how to generate size width and height in django?

article/models.py

class article(models.model):
	image1 = models.ImageField(upload_to=&#39;images&#39;, blank=True)

code head.html

&lt;meta property=&quot;og:image&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:secure_url&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:width&quot; content=&quot; &quot; /&gt;
&lt;meta property=&quot;og:image:height&quot; content=&quot; &quot; /&gt;```

add size height and width generate

答案1

得分: 0

Django的ImageField内置了这个功能。您只需添加两个新的列,用于宽度和高度:

class article(models.model):
    image1 = models.ImageField(width_field='image1_width', height_field='image1_height', upload_to='images', blank=True)
    image1_width = models.IntegerField(blank=True, null=True)
    image1_height = models.IntegerField(blank=True, null=True)

然后您可以简单地进行如下操作:

<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content="{{ article.image1_width }}" />
<meta property="og:image:height" content="{{ article.image1_height }}" />
英文:

Django ImageField has a built-in feature for this. You just need to add two new columns for width and height:

class article(models.model):
    image1 = models.ImageField(width_field=&#39;image1_width&#39;, height_field=&#39;image1_height&#39;, upload_to=&#39;images&#39;, blank=True)
    image1_width = models.IntegerField(blank=True, null=True)
    image1_height = models.IntegerField(blank=True, null=True)

Then you can simply do:

&lt;meta property=&quot;og:image&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:secure_url&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:width&quot; content=&quot;{{ article.image1_width }}&quot; /&gt;
&lt;meta property=&quot;og:image:height&quot; content=&quot;{{ article.image1_height }}&quot; /&gt;```

答案2

得分: 0

我的 models.py

class article(models.model):
    image1 = models.ImageField(upload_to='images', width_field='image1_width', height_field='image1_height')
    image1_width = models.IntegerField(editable=False, null=True)
    image1_height = models.IntegerField(editable=False, null=True)

我的代码中的 head.html:

<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content="{{ article.image1_width }}" />
<meta property="og:image:height" content="{{ article.image1_height }}" />
英文:

my models.py

class article(models.model):
image1 = models.ImageField(upload_to=&#39;images&#39;, width_field=&#39;image1_width&#39;, height_field=&#39;image1_height&#39;)
image1_width = models.IntegerField(editable=False, null=True)
image1_height = models.IntegerField(editable=False, null=True)

my code head.html

&lt;meta property=&quot;og:image&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:secure_url&quot; content=&quot;{{ article.image1 }}&quot; /&gt;
&lt;meta property=&quot;og:image:width&quot; content=&quot;{{ article.image1_width }}&quot; /&gt;
&lt;meta property=&quot;og:image:height&quot; content=&quot;{{ article.image1_height }}&quot; /&gt;

huangapple
  • 本文由 发表于 2023年2月16日 09:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定