django - 阻止评估模板标签但不评估变量
问题描述
我正在使用 Django 的模板引擎来评估用户提供的模板字符串。我想允许用户使用变量映射功能,而不是标签或过滤器。
例如:
from django.template import Context, Template
user_template_string = "V1: {{ var1 }}. V2: {{ var2|truncatechars:5 }}. {% if var3 %} yes {% else %} no {% endif %}"
template = Template(user_template_string)
context = {'var1': 'One',
'var2': '123456789',
'var3': True}
output = template.render(context=Context(context))
# Desired output:
# "V1: One. V2: 123456789. {% if var3 %} yes {% else %} no {% endif %}"
有没有办法将模板引擎配置为呈现变量但忽略标签和过滤器?或者我最好的选择是清理用户输入并尝试去除它们可能包含的所有标签和过滤器?
解决方案
推荐阅读
- java - 无法在控制器中同时添加两个功能
- java - Apache spark和scala,执行查询时出错
- php - 使用 Codeigniter 成功验证时重定向警报
- sqlalchemy - 什么是基于查询过滤器返回对象(代表数据库中的一行)的 SQLAlchemy (ORM) 命令?
- r - do.call 和 rbind 用于列表,顺序为新列名
- oracle - 排除主页的授权 - Oracle Apex
- python - 如何从我的 Python 列表中选择其他选项
- java - JUnit 5 - 新注解 - 使用 JDB 进行调试
- python-3.x - 为什么 filter + abs 函数没有给出正确的值
- java - 我如何在后台关闭我的 android 应用程序的所有活动