Here is the task I try to solve:
- Add a column to a given pandas table and add some links to it.
- 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}¶m2={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¶m2=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?