【发布时间】:2019-01-22 06:51:27
【问题描述】:
如何让 Django (1.11) 在渲染过程中忽略模板中缺失的变量?我需要在多个步骤中使用不同的数据呈现相同的模板。我需要将 Django 模板引擎用于它包含的所有功能,并且我无法修改模板。
而不是用空字符串替换它们:
>>> from django.template import Template, Context, TemplateSyntaxError
>>> c = Context({'foo': 'hello'})
>>> t = Template('{{foo}} {{bar}}')
>>> t.render(c)
'hello '
我希望它们保持原样
>>> t.render(c)
'hello {{bar}}'
【问题讨论】: