Django-tables2 添加 html 属性到列

Django-tables2 add html attribute to column

我在模型中有一个图像字段,在我的 table 中该字段显示为 link 但是当您单击它时,它会指向 url 而不是我想下载它。我发现在 html 中你可以像这样添加 "download" 属性:

<a href="/media/pictures/CFE.JPG" download></a>

所以在我的 django-tables2:

tables.py

class PagosDetailTable(tables.Table):

    imagen = tables.Column(
        attrs={"td": {"href": "download"}})

    class Meta:
        model = Pagos
        template_name = "django_tables2/bootstrap-responsive.html"
        fields = ('carro', 'semana', 'fecha', 'pago', 'imagen')
        attrs = {"class": "table table-hover table-sm"}

但它覆盖了 href:

<a href="download"></a>

有没有办法使用 tables.Column 附加下载属性?

我找到了解决问题的方法:

tables.py

class ImageColumn(tables.Column):
    def render(self, value):
        return format_html('<a href="/media/{}" download>Imagen</a>', value)


class PagosDetailTable(tables.Table):
    imagen = ImageColumn()

    class Meta:
        model = Pagos
        template_name = "django_tables2/bootstrap-responsive.html"
        fields = ('carro', 'semana', 'fecha', 'pago')
        attrs = {"class": "table table-hover table-sm"}