gpt4 book ai didi

python - flask 类型错误 : 'ImmutableMultiDict' object is not callable

转载 作者:行者123 更新时间:2023-11-28 04:47:38 25 4
gpt4 key购买 nike

有人可以说这段代码有什么问题吗?我得到的错误:

TypeError: 'ImmutableMultiDict' object is not callable

def login():
error = None
form = LoginForm(request.form)

if request.method == 'POST':
viewer = User.query.filter_by(Group='viewer').first()
admin = User.query.filter_by(Group='admin').first()
staff = User.query.filter_by(Group='staff').first()
user = User.query.filter_by(Email=request.form(['email'])).first()
password = User.query.filter_by(Password=request.form(['password'])).first()
if (user or password) is None:
session['logged_in'] = False
flash('Please write your username password')
else:

session['logged_in'] = True
flash('You were logged in')
if viewer:
return redirect(url_for('viewer'))
elif admin :
return redirect(url_for('admin'))
elif staff:
return redirect(url_for('employee'))

return render_template('login.html', form=form)

这是一个 html 部分:

<form class="sa-innate-form" method="post">
{{ form.csrf_token }}
<label>Email Address</label>
<input type="text" name="email" value="{{ request.form.email }}">
<label>Password</label>
<input type="password" name="password" value="{{ request.form.password }}">
<button type="submit" value="submit">Sign In</button>
<a href="">Forgot Password?</a>
</form>

最佳答案

这里有一个语法错误:

user = User.query.filter_by(Email=request.form(['email'])).first()

应该是:

user = User.query.filter_by(Email=request.form['email']).first()

还有一些其他的例子,所以确保你捕获了所有的例子。这是一个错误的原因是您正在尝试使用字符串列表 ['email'] 调用 request.form 而不是索引到表单中。

if (user or password) is None: 也是错误的。如果其中一个不是 None,则不会触发该 block 。但是,我相信理想的行为是同时要求两者。你可以试试这个:

if user is None or password is None:

或:

if None in [user, password]:

关于python - flask 类型错误 : 'ImmutableMultiDict' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901522/

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