17
我在我的模型中有ImageField
,我可以在模板中顯示圖像。但是,如何檢索圖像的高度和寬度?在Django模板中的ImageField上訪問圖像尺寸?
我在我的模型中有ImageField
,我可以在模板中顯示圖像。但是,如何檢索圖像的高度和寬度?在Django模板中的ImageField上訪問圖像尺寸?
請參閱ImageField的文檔。
除了可用於FileField字段的特殊屬性 ,一個 ImageField的也具有高度和寬度 屬性。
因此,只要做到以下幾點:
<img src="{{ image.url }}" width="{{ image.width }}" height="{{ image.height }}"/>
在我的情況下,爲width
和height
下地幹活,我需要設置的ImageField
class Product(models.Model):
image = models.ImageField(width_field='image_width', height_field='image_height')
image_width = models.IntegerField()
image_height = models.IntegerField()
嗯..它不工作在我的最後。 '{%與post.postimage_set.all作爲圖像%}' '' '{%ENDWITH%}' – 2013-10-03 11:57:07