python 如何使 Django 模板引擎在内存模板中呈现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2259743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Django template engine to render in memory templates?
提问by Jader Dias
I am storing my templates in the database and I don't have any path to provide for the template.render
method.
我将我的模板存储在数据库中,但我没有为该template.render
方法提供任何路径。
Is there any exposed method which accepts the template as a string?
是否有任何公开的方法接受模板作为字符串?
Is there any workaround?
有什么解决方法吗?
采纳答案by Ignacio Vazquez-Abrams
Instantiate Template
with the string to use as a template.
回答by robertzp
回答by Alexander Maretskiy
In Django < 1.8:
在 Django < 1.8 中:
from django.template.loader import get_template_from_string
tpl = Template(get_template_from_string("My name is {{ my_name }}."))