Reputation: 1146
I'm having trouble correctly passing a html table created with Pandas DataFrame.to_html to a Django view.
How do I do this correctly?
In views.py I have the function:
def my_view(request):
data_table = Class().main()
return render_to_response('app/insight.html', {'data_table':data_table})
Class().main() successfully returns a HTML table.
A simplified insight.html body would be:
<body>
{{data_table}}
</body>
The problem is that I'm getting source like:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>header 1:</th>
<th>header 2</th>
<th>header 3</th>
<th>header 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
</tbody>
</table>
And a page display of:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th>header 1</th> <th>header 2</th> <th>header 3</th> <th>header 4</th> </tr> </thead> <tbody> <tr> <td>data 1</td> <td>data 2</td> <td>data 3</td> <td>data 4</td> </tr> </tbody> </table>
Upvotes: 1
Views: 362
Reputation: 2796
you need to tell the template that it is safe to render html within a variable like this:
<body>
{{data_table|safe}}
</body>
related answer:https://stackoverflow.com/a/4848661/2174832
Upvotes: 2