0

Here is the task I try to solve:

  1. Add a column to a given pandas table and add some links to it.
  2. Render the table to html an show the table in a django template

Here is the code (Python 3.6 and Django 1.10.5):

views.py

def test(request):

    data = {
        'Age': ['23', '35'],
        'Name': ['Benny', 'Johny'],
    }

    table = pd.DataFrame(data)
    table.insert(loc=2, column='Link', value='')

    number1 = 1234
    number2 = 5678

    # This is not working
    link = f'<a href="/anotherview/?param1={number1}&param2={number2}">Click Me</a>'

    table.set_value(index=1, col='Link', value=link)
    htmltable = table.to_html(index=True, border=1, na_rep='', justify='left', escape=False)

    return render(request, 'test.html', {'table': htmltable})

Template: test.html

<!DOCTYPE html>
<html lang="de">
<head>
</head>
<body>

{% autoescape off %}
{{ table }}
{% endautoescape %}

<!-- This is working -->
<a href="/anotherview/?param1=1234&param2=5678">Click Me</a>


</body>
</html>

The problem is that the link doesn´t render properly. Here is a picture of the rendered HTML sourcecode: Rendered HTML

It works when I put the link directly into the HTML. I´m pretty sure it has something to do with djangos or pandas autoescaping or some kind of masking some characters (the '&') the wrong way, but i can´t figure out how to do it correctly. Who has an advice?

qfactor
  • 27
  • 9

1 Answers1

0

you should try the solution posted here:

How to display full (non-truncated) dataframe information in html when converting from pandas dataframe to html?

    pd.set_option('display.max_colwidth', -1)