Agil Yolchuyev Agil Yolchuyev - 9 days ago 4
Python Question

flask TypeError: 'ImmutableMultiDict' object is not callable

Hi guys Can someone say what is wrong with this code? The error which I get:
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)


And this is an html part:

<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>

Answer

You have a syntax error here:

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

Instead it should be:

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

There are a few other instances of this so make sure you catch all of them. The reason this is an error is you are trying to call request.form with the string list ['email'] instead of indexing into the form.

if (user or password) is None: is also buggy. If either one is not None, then the block will not be triggered. However, I believe that the desired behavior is to require both. You could try either this:

if user is None or password is None:

or:

if None in [user, password]: