Load django template from the database

前端 未结 2 1108
悲哀的现实
悲哀的现实 2020-12-31 09:22

Im trying to render a django template from a database outside of djangos normal request-response structure. But it appears to be non-trivial due to the way django templates

相关标签:
2条回答
  • 2020-12-31 09:44

    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)))
    
    0 讨论(0)
  • 2020-12-31 09:49

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

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

    0 讨论(0)
提交回复
热议问题