pythondjangocsvdjango-tables2django-filters

django-tables2 exporting custom URL column


G'day All,

Hope everyone is doing well.

I have a table that I render with a custom column that is a hyperlink.

The problem is when i export to CSV it just gives me the text of the hyperlink and not the hyperlink.

I'm wanting to export the hyperlink instead of just the text, is this possible? (if i need to switch to xlsx export that's also fine) (worse comes to worse I can just make the text the full path)

Custom column:

document_link = tables.TemplateColumn('<a href="{{record.document_file.url}}/" target="_blank">{{record.document_file}}</a>', verbose_name="Document location")  

Thanks in advance,


Solution

  • This was the final way how I managed to do it:

    class DocumentTable(ExportMixin,tables.Table):
    
        document_link = tables.TemplateColumn('<a href="{{record.document_file.url}}/" target="_blank">{{record.document_file}}</a>', verbose_name="Document location", exclude_from_export=True)  
        document_URL = tables.TemplateColumn('render_replaces_this',visible=False)
        
        def render_document_URL(self, value, record):
            return format_html(
                '{}{}',
                self.request.META['HTTP_HOST'],
                record.document_file.url,
            )