开发者

Accessing image dimensions on an ImageField in a Django template?

开发者 https://www.devze.com 2023-02-21 06:23 出处:网络
I have ImageField in my model and I\'m able to display th开发者_如何学JAVAe image in a template. However, how do I retrieve the image\'s height and width?See the documentation for ImageField.

I have ImageField in my model and I'm able to display th开发者_如何学JAVAe image in a template. However, how do I retrieve the image's height and width?


See the documentation for 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 }}"/>


In my case, for the width and height fields to work, I need to set the width_field and height_field of the ImageField

E.g.

class Product(models.Model):
    image = models.ImageField(width_field='image_width', height_field='image_height')
    image_width = models.IntegerField()
    image_height = models.IntegerField()
0

精彩评论

暂无评论...
验证码 换一张
取 消