使用 flask-wtforms 向 html 属性添加整数后缀或前缀

adding an integer postfix or prefix to an html attribute with flask-wtforms

我在 jinja2 for 循环中有以下内容:

{{ meal[item]['open-modal'].submit(**{ 'class':'btn btn-primary',
                                        'data-toggle':'modal', 
                                        'data-target':'#myModal' }) }}

我需要在数据目标上有一个索引,例如:

{{ meal[item]['open-modal'].submit(**{ 'class':'btn btn-primary', 
                                        'data-toggle':'modal', 
                                        'data-target':'#myModal-item' }) }}

item 是本例中需要的索引。有没有办法从 "ad-hoc dictionary" 中逃脱 item?以便它采用与 meal[item] 中相同的值?

我需要 'data-target' 属性呈现为 '#myModal-0''#myModal-1' 等。就目前而言,每个 'data-target' 属性都设置为 '#myModal-item'对于循环中的每个项目。换句话说,它将第二行代码中的 item 设置为字符串。

万一它对某人有帮助,最终解决我的问题是:

<form method="POST">
    {{ meal[item]['open-modal'].csrf_token }}
    {{ meal[item]['open-modal'].submit( **{ 'class':'btn btn-primary',
                                            'data-toggle':'modal',
                                            'data-target':'#myModal-' + 
                                                          item|string } ) }}
</form>

请记住,它嵌套在 jinja2 中的两个 for 循环中。

{% for meal in menu_dict %}
{% for item in meal %}
    ....
{% endfor %}
{% endfor %}

重点总结到这里,基本上就是:

'data-target':'#myModal-' + item|string

添加后缀。