gpt4 book ai didi

python - 具有多个参数的 app_template_filter

转载 作者:太空狗 更新时间:2023-10-29 21:23:58 26 4
gpt4 key购买 nike

如何将两个参数传递给 app_template_filter (doc) ?如果我只使用一个参数,这很有效。但在这种情况下,我需要两个。

@mod.app_template_filter('posts_page')
def posts(post_id, company_id):
pass

{{ post.id, post.company.id | posts_page }}

错误:

TypeError: posts_page() takes exactly 2 arguments (1 given)

最佳答案

虽然您可以使用上下文处理器,但它可能并不总是您想要的。

接受的答案中的文档片段说:

[Filters] may have optional arguments in parentheses.

因此,查看提问者的模板过滤器:

@mod.app_template_filter('posts_page')
def posts(post_id, company_id):
pass

以下内容在模板中有效:

{{ post.id|posts_page(post.company_id) }}

关于python - 具有多个参数的 app_template_filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com