How to make Django template engine to render in me

2020-05-25 05:18发布

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?

3条回答
【Aperson】
2楼-- · 2020-05-25 05:28

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)
查看更多
Viruses.
3楼-- · 2020-05-25 05:42

Instantiate Template with the string to use as a template.

查看更多
可以哭但决不认输i
4楼-- · 2020-05-25 05:48

In Django < 1.8:

from django.template.loader import get_template_from_string

tpl = Template(get_template_from_string("My name is {{ my_name }}."))
查看更多
登录 后发表回答