gpt4 book ai didi

flask - 如何自定义flask.ext.security.forms.LoginForm来提示和使用用户名而不是电子邮件?

转载 作者:行者123 更新时间:2023-12-03 16:37:54 25 4
gpt4 key购买 nike

如何自定义flask.ext.security.forms.LoginForm来提示和使用用户名而不是电子邮件?

我已经试过了

class ExtendedLoginForm(LoginForm):
name = TextField('User Name:', [Required()])
del LoginForm.email

security = Security(app, user_datastore, login_form=ExtendedLoginForm)


更改了模板,

<form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
{{ login_user_form.hidden_tag() }}
**{{ render_field_with_errors(login_user_form.name) }}**
{{ render_field_with_errors(login_user_form.password) }}
{{ render_field_with_errors(login_user_form.remember) }}
{{ render_field(login_user_form.next) }}
{{ render_field(login_user_form.submit) }}
</form>


更改了userdb以使用名称代替电子邮件

但是flask-security仍然尝试在数据存储区而不是用户中查找电子邮件。

最佳答案

在flask-security的早期版本中,用户的唯一标识符是email属性。您可以通过添加自己的属性来扩展模型,但是电子邮件字段仍然可以唯一地标识用户(因此,用户名必须是电子邮件字段)。
从1.7.0版开始,可以识别具有不同属性的用户。
在配置文件中,您应该添加

app.config['SECURITY_USER_IDENTITY_ATTRIBUTES'] = 'name'


但是您仍然必须在模型中具有email属性。
在git上报告了3天的问题,这正是您要查找的问题
https://github.com/mattupstate/flask-security/issues/396

关于flask - 如何自定义flask.ext.security.forms.LoginForm来提示和使用用户名而不是电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882485/

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