访问Django模板中ImageField上的图像尺寸?
问题描述:
我的模型中有 ImageField
,我可以在模板中显示图像。但是,如何检索图像的高度和宽度?
I have ImageField
in my model and I'm able to display the image in a template. However, how do I retrieve the image's height and width?
答
请参阅 ImageField 。
除了可用于FileField的特殊属性
之外,
ImageField还具有高度和宽度
属性。
In addition to the special attributes that are available for FileField, an ImageField also has height and width attributes.
所以只需执行以下操作:
So just do the following:
<img src="{{ image.url }}" width="{{ image.width }}" height="{{ image.height }}"/>