How to use sorl-thumbnail package together with django-tables2 package?

64 views Asked by At

I do have a table defined in tables.py that renders a column with recipes images.

class ImageColumn(Column):

    def render(self, value):
        return format_html(
            '<img src="static/media/{url}" height="150px", width="150px">',
            url=value
        )


class RecipeTable(tables.Table):

    image = ImageColumn()
    name = Column(linkify=True)

    class Meta:
        model = Recipe
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            "image",
            "name",
            "directions",
            "ingredients",
        )

Each image has different size and when I render it with fixed height="150px", width="150px", aspect ratio messes up the image. Therefore I thought I could use sorl-thumbnail package to help mi with generating thumbnails, rather then resizing the whole images. It looks like it is not possible to easily use both django-tables2 and sorl-thumbnail since thumbnails are rendered in html template. My template contains only this to render the table:

{% render_table table 'django_tables2/bootstrap4.html' %}

I need to access the cell so that I can use thumbnail template tag where image should be placed.

{% thumbnail item.image ‘200x100’ as im %}
    <img src=’{{ im.url }}’>
{% endthumbnail %}

The only solution I see could be to edit the bootstrap4.html, but is there a better way? Am I missing something?

0

There are 0 answers