1

有一个烧瓶 wtforms 选择字段并尝试合并 htmx ajax 调用,它在数据属性中有破折号,所以我在 SO 上找到了一个解决方案,如下所示:

{{ form.dia(class="form-select", **{'hx-post': "/pulleys/{{pulley_id}}/hub_type", 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }} 

但是模板没有解析{{pulley_id}},我猜它已经在另一个{{}}中了。有没有一种方法可以构建如上所示的动态部分,所以它最终成为

'hx-post'="/pulleys/1/hub_type"

当 pulley_id=1 完全渲染时

4

1 回答 1

1

如果pulley_id是循环内的变量或传递给render_template您的变量应该能够格式化字符串:

{{ form.dia(class_="form-select", **{'hx-post': "/pulleys/%s/hub_type"|format(pulley_id), 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }} 

注意:如果您尝试设置 HTML 类属性,您也class_不想这样做。class

于 2021-10-29T01:24:09.070 回答