Search code examples
pythondjangodjango-modelsdjango-tables2

Django tables2 resize image in cell


I want to resize the image that is being shown in the first column of this table in django. The table is based on django-tables2.

model.py

class Staff(models.Model):
   name = models.CharField(max_length=30, default='')
   staff_picture = models.ImageField(upload_to = 'staff_images/', default = 'staff_images/no-img.png')


table.py

class ImageColumn(tables.Column):
    def render(self, value):
        return mark_safe('<img src="/media/%s" />' % escape(value))

class Staff(tables.Table):
    doc_pic1 = ImageColumn('staff_picture')
    class Meta:
        model = Staff
        attrs = {"class": "paleblue"}
        fields = ('staff_picture',"Name")

I have no clue how to do it. Any help is much appreciated.


Solution

  • I just realized the solution is super simple. I can write directly into the img tag.

    class ImageColumn(tables.Column):
        def render(self, value):
            return mark_safe('<img src="/media/%s" height="50" />' % escape(value))