[Fixed]-Load django template from the database

45👍

There’s nothing complicated about this, and it doesn’t have anything to do with the request/response structure. All you need to do is pass the template string into the django.template.Template constructor (BTW, I’ve changed the name of your model, to avoid confusion):

from django.template import Context, Template
from myapp.models import DbTemplate

s = DbTemplate.objects.get(pk=123).content
tpl = Template(s)
tpl.render(Context(dict(a=123, b=456)))

10👍

There is a reusable app which loads templates from the database:

http://django-dbtemplates.readthedocs.org/en/latest/

Leave a comment