大家好,有人可以说这段代码有什么问题吗?我得到的错误: TypeError:' ImmutableMultiDict'对象不可调用
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>
答案 0 :(得分:4)
这里有语法错误:
user = User.query.filter_by(Email=request.form(['email'])).first()
相反它应该是:
user = User.query.filter_by(Email=request.form['email']).first()
还有其他几个例子,所以一定要抓住所有这些。这是一个错误的原因是您尝试使用字符串列表request.form
调用['email']
而不是索引到表单中。
if (user or password) is None:
也有错误。如果其中任何一个不是None
,则不会触发该块。但是,我认为理想的行为是要求两者兼顾。你可以试试这个:
if user is None or password is None:
或:
if None in [user, password]: