英文:
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='images', blank=True)
code head.html
<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content=" " />
<meta property="og:image:height" content=" " />```
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='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)
Then you can simply do:
<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 }}" />```
答案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='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)
my code 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 }}" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论