Display an HTML table from a Django view in another template

So, I have a Django view that allows users to upload a data file to be copied using a Pandas dataframe. Then the graph is created in a separate view that is referenced in the original file upload view through the image tag. It works great.

Now I would also like to display the data in a table format next to the graph image. I use Panda built in dataframe.to_html()to create a table in a separate view, which will also be specified in the file download view.

I created the table correctly and can be seen at the URL specified in the table. I encounter an error when I try to link to this URL from a file upload template.

Error:

Resource interpreted as Image but transferred with MIME type text/html

I found that this is because my file upload template has the following line:

<img src="{% url 'html_graph' %}" />

since the view returns HttpResponsewith content_typefrom text/html.

Is there a way to reference this view of an HTML table from my file upload template that does not use an image tag, since the table is HTML, not an image?

Decision

Here is my working implementation:

html_table = df.to_html(index=False)
return render_to_response('myapp/my_template.html', {'html_table': html_table}, RequestContext(request))

Then I refer to html_tablein my template, for example:

<div>
 {{ html_graph | safe }}
</div>
+4
source share

All Articles