2010-02-14 242 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)