我有一个flask应用程序,可将用户数据添加到MySQL数据库。我脚本上的所有内容似乎都能正常运行。我想要的数据已添加到MySQL数据库。但是我收到以下错误:
127.0.0.1 - - [28/Jun/2018 12:19:44] "GET /protected HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1831, in finalize_request
response = self.make_response(rv)
File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1957, in make_response
'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
我无法弄清楚为什么会出现此错误。
这是我的代码:
from flask import Flask, request, jsonify, session
from flask_restful import Resource, Api
from werkzeug.security import generate_password_hash, check_password_hash
import mysqldb
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token,
get_jwt_identity
)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
jwt = JWTManager(app)
api = Api(app, prefix="/api/v1")
USER_DATA = {
"user1": "abc123",
"user2":"userpass"
}
@app.route('/login', methods=['POST'])
def login():
if not request.is_json:
return jsonify({"msg": "Missing JSON in request"}), 400
username = request.json.get('username', None)
password = request.json.get('password', None)
if not username:
return jsonify({"msg": "Missing username parameter"}), 400
if not password:
return jsonify({"msg": "Missing password parameter"}), 400
if username != USER_DATA.get(username) and password not in USER_DATA[username]:
return jsonify({"msg": "Bad username or password"}), 401
access_token = create_access_token({'first_name': request.json.get('Client First Name', None), 'last_name': request.json.get('Client Last Name', None)})
return jsonify(access_token=access_token), 200
class PrivateResource(Resource):
@app.route('/protected', methods=['GET'])
@jwt_required
def sendData():
data = get_jwt_identity()
return mysqldb.addUser("{}".format(str(data['first_name'])),"{}".format(str(data['last_name'])))
if __name__ == '__main__':
app.run(debug=True)
addUser
函数是这样的:
def addUser(Fname,Lname):
try:
cursorObject = connection.cursor()
cursorObject.execute("call sp_createUser('{}'".format(Fname)+",'{}')".format(Lname))
connection.commit()
connection.close()
finally:
print('User data added succesfully!')
代码中导致错误的错误是什么?
答案 0 :(得分:2)
/protected
函数正在处理您的sendData
路径,该函数返回mysqldb.addUser
的结果:
return mysqldb.addUser(...)
该函数返回的内容均不是有效的HTTP响应,因此您会收到错误消息。
编辑:
现在,您添加了该函数,我们可以看到它什么也不返回。因此,您的sendData
函数将返回None
,这不是发送给客户端的有效响应。