Dark Launch

Django template render string as html

Render a template filename with context to an html string in Django.

Python
from django.template.loader import render_to_string
html = render_to_string('relative/path/to/my_template.html', {'foo': 'bar'})

Render template from a string.

Python
>>> from django.template import Context
>>> from django.template import Template
>>> template = Template('Hello, {{ thing }}!')
 
>>> context = Context({'thing': 'World'})
>>> template.render(context)
u'Hello, World!'
 
>>> context = Context({'thing': 'Internet'})
>>> template.render(context)
u'Hello, Internet!'