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)
就我而言,要使width和height字段起作用,我需要设置width_field和height_field的ImageField
例如
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)