python - Django 模板内置过滤器 : Using a variable value in an argument

标签 python django django-templates

使用 Django 的内置 yesno过滤器,我需要插入以下值之一:

  1. “我”这个词
  2. 变量owner_name的值

这是我尝试在我的模板中使用的代码:

"Look what {{ is_owner|yesno:"I,{{ owner_name }}" }} created!"

使用上面的代码会导致以下错误:

Could not parse the remainder: ':"I,{{ owner_name' from 'is_owner|yesno:"I,{{ owner_name'

那么我该如何转义过滤器参数中的变量呢?

最佳答案

您可以做的另一件事是使用 with 模板标签和 add 过滤器连接参数:

{% with yesno_args="I,"|add:owner_name %}
"Look what {{ is_owner|yesno:yesno_args }} created!"
{% endwith %}

我认为这比必须添加自定义过滤器更简单。

关于python - Django 模板内置过滤器 : Using a variable value in an argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663508/

相关文章:

python - @classmethod 和 python 中方法的区别

python - Django 应用程序的 Heroku 部署失败

django 在模板中使用媒体 URL

python - 从 headless 树莓派使用 simplecv 运行 python 脚本

python - 在 Python 中打开远程文档

Python 线程和 GIL

python - Django - 重建没有变量之一的查询字符串

python - 将自定义按钮添加到 django cms ckeditor

django-templates - 谷歌可视化-条形图上的点击事件是Stacked : true

python - 属性错误 : 'function' object has no attribute 'views'