如何在模板 Jinja2 的同一对象上添加 class 和其他 HTML 属性?

How to add a class and other HTML attributes on the same object on template Jinja2?

考虑下面的 HTML 部分代码:

<tr>
    <td>
        <label>IVA</label>
    </td>
    <td>
        {{form.iva(class="form-control")}}
        {% if form.iva.errors %}<span class="error">{{';'.join(form.iva.errors)}}</span>{%endif%}
    </td>
</tr>

这部分 python 代码 (Flask/Wtforms):

iva = SelectField(u"IVA", [validators.Required()], choices=[])

除了设置 class "form-control" 我想添加更多样式但不更改 class,换句话说,只需在这个特定对象中添加更多样式, 例如: 宽度: 150px;

那么,我该怎么做?

您可以渲染字段添加一些关键字参数,这些关键字参数将作为 html 属性注入,例如 styleplaceholder自动完成,等等:

<tr>
<td><label>IVA</label></td>
<td>
  {{form.iva(class="form-control",style="min-width: 150px; max-width: 200px;")}}
  {% if form.iva.errors %}<span class="error">{{';'.join(form.iva.errors)}}</span>{%endif%}
</td>
</tr>

official documentation

中查看更多信息