TypeError:“ dict”对象不可调用?

时间:2019-03-20 10:08:49

标签: python dictionary flask request

我正在尝试构建我的第一个烧瓶应用程序,当我尝试从该表单发送登录数据时:

<form class="border border-light p-5" action="http://localhost:5000/login" method="POST">

    <p class="h4 mb-4 text-center">Connexion</p>
    <input type="email" id="defaultLoginFormEmail" class="form-control mb-4" placeholder="E-mail" name="username">
    <input type="password" id="defaultLoginFormPassword" class="form-control mb-4" placeholder="Mot de passe" name="password">
    <div class="d-flex justify-content-between">
    </div>
    ...
</form>

路由映射:

@app.route('/login', methods=['POST'])
def UserLogin():
    credentials={}
    credentials['username'] = request.form['username']
    credentials['password'] = request.form['password']
    #I think that the problem in the 3 lines above
    current_user = UserModel.find_by_username(credentials['username'])
    if not current_user:
        return {'message': 'User {} doesn\'t exist'.format(credentials['username'])}

    if UserModel.verify_hash(credentials['password'], current_user.password):
        access_token = create_access_token(identity = credentials['username'])
        refresh_token = create_refresh_token(identity = credentials['username'])
        return redirect(url_for('search'))
    else:
        return {'message': 'Wrong credentials'}

当我尝试登录时,我得到:TypeError: 'dict' object is not callable

按要求进行完全回溯:

Traceback (most recent call last):
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "C:\Users\acer\Anaconda3\lib\site-packages\flask\app.py", line 1740, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "C:\Users\acer\Anaconda3\lib\site-packages\werkzeug\wrappers.py", line 921, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "C:\Users\acer\Anaconda3\lib\site-packages\werkzeug\test.py", line 923, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'dict' object is not callable

1 个答案:

答案 0 :(得分:2)

您正在尝试返回dict作为响应。 -尝试使用如下所示的内容。

from flask import jsonify

@app.route('/route')
def example():
    return jsonify({"your": "dict"})