在Django模板中访问ImageField上的图像尺寸?

ole*_*mil 21 django image dimensions

我有ImageField我的模型,我能够在模板中显示图像.但是,如何检索图像的高度和宽度?

bra*_*ers 30

请参阅ImageField的文档.

除了可用于FileField的特殊属性外,ImageField还具有height和width属性.

所以只需执行以下操作:

<img src="{{ image.url }}" width="{{ image.width }}" height="{{ image.height }}"/>
Run Code Online (Sandbox Code Playgroud)


Ron*_*Ron 5

就我而言,要使widthheight字段起作用,我需要设置width_fieldheight_fieldImageField

例如

class Product(models.Model):
    image = models.ImageField(width_field='image_width', height_field='image_height')
    image_width = models.IntegerField()
    image_height = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)