【发布时间】:2021-03-31 13:32:17
【问题描述】:
我也使用 bs4 实现了烧瓶安全的客户模板。 流程是当用户登录和错误的电子邮件/密码时,我将向用户显示无效反馈。我为每个字段都实现了宏,这是我的 sc:
login_form.html
{% extends "security/layout.html" %}
{% from "security/macro/auth_message.html" import auth_message as am %}
{% from "security/macro/forms.html" import render_field_with_errors, render_field, render_field_errors,
render_field_with_errors_no_labels, render_field_with_errors_no_labels_without_input_group %}
{% block title %}{{ 'login page' | title }}{% endblock title %}
{% block content %}
{{ am("Login Form") }}
{% include "security/macro/alert.html" %}
<form class="form-signin" action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
{{ login_user_form.hidden_tag() }}
{{ render_field_with_errors_no_labels(login_user_form.email, class_="form-control", id="email", placeholder="Email
address") }}
{{ render_field_with_errors_no_labels(login_user_form.password,class_="form-control", id="password",
placeholder="Password") }}
<div class="row">
<div class="col-8">
<div class="icheck-primary">
{{ render_field_with_errors_no_labels_without_input_group(login_user_form.remember, id="remember") }}
<label for="remember">
Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-4">
{{ render_field(login_user_form.submit,class="btn btn-primary btn-block" ) }}
</div>
<!-- /.col -->
</div>
<hr>
{{ render_field_errors(login_user_form.csrf_token) }}
</form>
{% endblock %}
security/macro/forms.html
{% macro render_field_with_errors_no_labels(field) %}
<div class="input-group mb-3">
{{ field(**kwargs)|safe }}
{% if field.errors %}
{# this section i have trouble #}
{% set field = field.html_params({'class_':'is-invalid', 'aria-describedby':'{{ field.id }}-error'}) %}
<span id="{{ field.id }}-error" class="invalid-feedback">
{% for error in field.errors %}
{{ error }}
{% endfor %}
</span>
{% endif %}
</div>
{% endmacro %}
但结果是
jinja2.exceptions.UndefinedError: 'wtforms.fields.simple.PasswordField object' has no attribute 'html_params'
那么,如何使用 **kwargs 更新字段以添加指定的类和属性?
谢谢
【问题讨论】: