0

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,

2 Answers2

1

I would suggest making use of "Including and Excluding Columns" https://django-tables2.readthedocs.io/en/latest/pages/export.html#including-and-excluding-columns

Add exclude_from_export=True to the document_link field, and an invisible document_link_for_csv field.

class DocumentTable(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_link_for_csv = columns.Column(
        visible=False, 
        accessor="document_file__url"
    )

Update based on comment: You can pass the request through to the table, and then do a custom render and request.build_absolute_uri().

class DocumentTable(tables.Table):

    document_link = tables.Column(
        verbose_name="Document location", 
        exclude_from_export=True
    )
    document_link_for_csv = columns.Column(
        visible=False, 
        accessor="document_file__url"
    )

    def render_document_link_for_csv(self, value, record):
        return format_html(
            '<a href="{}/{}/" target="_blank">{}</a>,
            request.build_absolute_uri(),
            record.document_file.url,
            record.document_file
        )
Ben
  • 2,348
  • 1
  • 20
  • 23
  • I did think about this however my document_file__url will still only return the ending how would i include the initial website as well? in this case local host? unless I just manually type in the link (think) – Thomas Lewin Feb 02 '22 at 10:29
  • @ThomasLewin Yes, I would likely just drop in the first part of the url. Otherwise you could pass the request into the table, and then use `request.build_absolute_uri()` for the full url. – Ben Feb 03 '22 at 01:46
  • 1
    @ThomasLewin I've updated the answer with what should work for you. – Ben Feb 03 '22 at 01:57
  • I'll give it ago in the next few days and let you know the result, thanks for help Ben! it is much appreciated. – Thomas Lewin Feb 03 '22 at 04:52
  • This did not work, still just returning the URL without the initial website. I think I should just hardcode it in. – Thomas Lewin Feb 17 '22 at 05:23
  • I'm trying this: `document_URL = tables.TemplateColumn('{{ request.get_host }}{{record.document_file.url}}',visible=False)` In the view it returns the correct path but when I download the CSV it doesnt show {{ request.get_host }} Maybe I need to look into django-tables2 and why it does that.. maybe I need to edit something in the view response. – Thomas Lewin Feb 17 '22 at 05:28
  • https://django-tables2.readthedocs.io/en/latest/pages/export.html "What exactly is exported?" It talks about overriding the value however I don't know how to do that... :\ – Thomas Lewin Feb 17 '22 at 05:33
  • 1
    I'm just seeing that I misnamed the `render_foo()` method (fixed to render_document_link_for_csv, not render_id), which may or may not be why it didn't work for you. – Ben Feb 17 '22 at 08:45
  • 1
    Overriding the value means that similar to render_document_link_for_csv() for custom table cell _rendering_, you'd make a value_foo > value_document_link_for_csv() method to override the _value_ of the cell. On that note, if the fix of renaming to render_document_link_for_csv doesn't work, you might try renaming the method to value_document_link_for_csv(). – Ben Feb 17 '22 at 08:46
0

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,
        )