django - Django register.tag 将动态变量作为 kwarg
问题描述
我注意到register.simple_tag
可以很容易地实现这一点,但不幸的是我现在无法切换到使用它并想用它register.tag
来实现它
有一个标签:
register.tag(name='foo')
def foo(parser, token):
bits = token.split_contents()
# rest of code..
模板,我有一个我们称之为bar
需要传递给foo
. 不幸的是,我似乎无法获得动态值 - 下面的 1. 将只是一个字符串 2. 将是一个字符串
1.
{% foo arg='bar' %}
{{ foo_* }}
{% endfoo %}
2.
{% foo arg=bar %}
{{ foo_* }}
{% endfoo %}
看起来我可以
{% with b=bar %}
{% foo arg=b %}
{{ foo_* }}
{% endfoo %}
{% endwith %}
我希望不必在with
这里使用语句,而是让“令牌”与 bar 对象一起出现,而不仅仅是获取它的字面量。
解决方案
推荐阅读
- python-3.x - SymPy 限制符号
- html - 我在子域中的 URL 不会指向主域
- python - 有没有办法通过脚本操作 tkinter GUI?
- haskell - 程序 'llvm-config' 版本 ==9.0.* 是必需的,但找不到
- rust - 一旦 tokio::process::Child 退出而不关闭 stdin,如何创建完成的未来
- javascript - 证书链中的自签名证书?
- python - 创建类的实例 N 次而不将其分配给变量
- python - 使用 peewee 在 SQLite 上创建两个链接表的 bulk_create():主键未更新
- python - 由于未在所有 CPU 内核中充分利用,Python Pandas 合并大型数据集的速度很慢,用于在两个数据帧中查找共同元素
- mysql - 在字符串中查找子字符串,如果确实存在,则将其插入另一个表 SQL