【问题标题】:Django-tables2 add html attribute to columnDjango-tables2 将 html 属性添加到列
【发布时间】:2020-03-02 18:58:01
【问题描述】:

我在模型中有一个 Imagefield,在我的表格中,该字段显示为链接,但是当您单击它时,它会指向 url,而我想下载它。我发现在 html 中你可以像这样添加“下载”属性:

<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】:

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

    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"}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    相关资源
    最近更新 更多