36

I am storing my templates in the database and I don't have any path to provide for the template.render method.

Is there any exposed method which accepts the template as a string?

Is there any workaround?

Jader Dias
  • 88,211
  • 155
  • 421
  • 625

3 Answers3

80

Based on the the docs for using the template system:

from django.template import Template, Context

t = Template("My name is {{ my_name }}.")
c = Context({"my_name": "Adrian"})
t.render(c)
natevw
  • 16,807
  • 8
  • 66
  • 90
robertzp
  • 1,415
  • 1
  • 13
  • 10
19

Instantiate Template with the string to use as a template.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    It's bad to have links that can/will die. Please update, or the answer below should be the accepted answer – BjornW Nov 11 '19 at 15:26
5

In Django < 1.8:

from django.template.loader import get_template_from_string

tpl = Template(get_template_from_string("My name is {{ my_name }}."))
meshy
  • 8,470
  • 9
  • 51
  • 73
  • 6
    From the [Django 1.8 docs](https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/#get-template-from-string): "Private API `get_template_from_string(template_code)` was removed in Django 1.8 because…" – natevw Aug 15 '15 at 06:37