Search code examples
djangodjango-tables2

Put a link in the footer Django Tables 2


I'm trying to add a footer to my table that is a link. I have found you add a footer like this:

ID = tables.Column(footer=(''))

but I'm not sure how to make what's in the quotes a link. Thanks!


Solution

  • If you look at the django-tables2 documentation for footers, You'll see something like you mentioned in your question:

    class Table(tables.Table):
        country = tables.Column(footer='')
    

    (Note that you can pass a string or a callable, not a tuple like your example does.)

    Django-tables2 will take this value and make sure to remove all formatting before rendering the value you pass. So if you want to render an url, you need to make explicit you want it to render the passed value as is. You can use django.utils.html.format_html() for that:

    country_footer_url = reverse('country_reference')  # or use a string containing your url
    
    
    class Table(tables.Table):
        country = tables.Column(
            footer=format_html('<a href="{}">country reference</a>', country_footer_url
        )