Django - get HTML output into a variable

Srikar Appalaraju picture Srikar Appalaraju · Jan 6, 2011 · Viewed 12.9k times · Source

instead of using render_to_response which will send the HTML output back to browser.

I would like to take the results, generate HTML (using templates) & output the html into a variable in my views.py. How can I do this?

Answer

Srikar Appalaraju picture Srikar Appalaraju · Mar 4, 2017

SOLVED! the way to do this -

from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', { 'foo': 'bar' })

thanks to ned for pointing to the Django docs