【问题标题】:How to make Django template engine to render in memory templates?如何让 Django 模板引擎在内存模板中呈现?
【发布时间】:2011-01-16 14:44:57
【问题描述】:

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

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

有什么解决办法吗?

【问题讨论】:

    标签: python django google-app-engine templates django-templates


    【解决方案1】:

    基于the docs使用模板系统:

    from django.template import Template, Context
    
    t = Template("My name is {{ my_name }}.")
    c = Context({"my_name": "Adrian"})
    t.render(c)
    

    【讨论】:

      【解决方案2】:

      Instantiate Template 带有用作模板的字符串。

      【讨论】:

      • 拥有可以/将会死掉的链接是很糟糕的。请更新,否则下面的答案应该是公认的答案
      【解决方案3】:

      在 Django

      from django.template.loader import get_template_from_string
      
      tpl = Template(get_template_from_string("My name is {{ my_name }}."))
      

      【讨论】:

      • 来自 Django 1.8 docs:“私有 API get_template_from_string(template_code) 在 Django 1.8 中被删除,因为……”
      猜你喜欢
      • 2011-07-17
      • 2019-11-24
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      相关资源
      最近更新 更多