Django-tables2将html属性添加到列中

问题描述 投票:0回答:1

我在模型中有一个Imagefield,在我的表中该字段显示为链接,但是当您单击它时,它会指向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附加下载属性?

django django-models django-tables2
1个回答
0
投票

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

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"}
© www.soinside.com 2019 - 2024. All rights reserved.