stev
stev

Reputation: 93

Dynamically rendering href in DataTables with Jinja2

I am using Jinja2 with FastAPI.

I would like to use a template table made with DataTables wherein I need to customize the table values for different pages using the same template table.

In one of the tables I have to convert the data of first column into a link (href tag).

For this I use the render method of DataTables for columndefs so that in my data-table.html I have:

new DataTable('#myTable', {
    ...
    columnDefs: [{{ columndefs|e }}]
});

and in the parent template I set columndefs as local variable:

{% set columndefs = """
        {
            targets: 0,
            render: (data, type, row) => '<a href="/api/'+data+'">'+data+'</a>',
        }
    """
%}
...
{% include "data-table.html" %}

The problem is that this throws an error:

    {% set columndefs = """
jinja2.exceptions.UndefinedError: 'api' is undefined

I think this is because the templating has problems by mixed use of " and '.

How can I correctly use href in columndefs so that it works?

Upvotes: 0

Views: 33

Answers (0)

Related Questions