How to make Django template engine to render in memory templates?

折月煮酒 提交于 2020-01-12 02:58:54

问题


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?


回答1:


Instantiate Template with the string to use as a template.




回答2:


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)



回答3:


In Django < 1.8:

from django.template.loader import get_template_from_string

tpl = Template(get_template_from_string("My name is {{ my_name }}."))


来源:https://stackoverflow.com/questions/2259743/how-to-make-django-template-engine-to-render-in-memory-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!