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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:12:06  来源:igfitidea点击:

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

pythondjangogoogle-app-enginetemplatesdjango-templates

提问by Jader Dias

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

我将我的模板存储在数据库中,但我没有为该template.render方法提供任何路径。

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

是否有任何公开的方法接受模板作为字符串?

Is there any workaround?

有什么解决方法吗?

采纳答案by Ignacio Vazquez-Abrams

Instantiate Templatewith the string to use as a template.

Template使用字符串进行实例化以用作模板。

回答by robertzp

Based on the the docsfor 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)

回答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 }}."))