2010-02-14 267 views

回答

4
from django.template.loader import get_template_from_string 

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

从[Django的1.8文档](https://docs.djangoproject.com/en/1.8/ref/templates/upgrading /#get-template-from-string):“私有API'get_template_from_string(template_code)'在Django 1.8中被删除,因为......” – natevw 2015-08-15 06:37:48

22

基于该the docs使用模板系统:

from django.template import Template, Context 

t = Template("My name is {{ my_name }}.") 
c = Context({"my_name": "Adrian"}) 
t.render(c)