Flask-restful 路由函数的用法是否正确?
The usage of Flask-restful routing functions is correct?
我可以访问 /v1/folder
但无法访问 /v1/folder/<folder-id>
。你能告诉我原因吗?在flask-request document中表示add_resource()
可以路由多个URI。但是我不能。也许我误解了什么。如果你找到线索,请告诉我。
from flask import request
from flask_restful import Resource, abort
class Folder(Resource):
def post(self, folder_id):
return { "message":"post with folder_id"}, 200
def post(self):
return { "message":"post without folder_id"}, 201
app = Flask(__name__)
.....
api_bp = Blueprint('api', __name__)
api = Api(api_bp, serve_challenge_on_401=True)
api.add_resource( Folder, '/v1/folder', '/v1/folder/<string:folder_id>')
app.register_blueprint(api_bp)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True )
错误消息是 "TypeError: post() got an unexpected keyword argument 'folder_id' "。怎么了?
Python 不支持 function/method 重载,因此您最后声明的 post
方法总是会被使用。相反,您应该使用 Python 提供的工具 - 参数的默认值。
我个人会做以下事情:
from flask import request
from flask_restful import Resource, abort
class Folder(Resource):
def post(self, folder_id=None):
if folder_id is None:
return self.__simple_post()
else:
return self.__parameter_post(folder_id)
def __parameter_post(self, folder_id):
return { "message":"post with folder_id"}, 200
def __simple_post(self):
return { "message":"post without folder_id"}, 201
app = Flask(__name__)
.....
api_bp = Blueprint('api', __name__)
api = Api(api_bp, serve_challenge_on_401=True)
api.add_resource( Folder, '/v1/folder', '/v1/folder/<string:folder_id>')
app.register_blueprint(api_bp)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True )
或者您可以处理 post
方法中的逻辑,前提是逻辑足够相似且不太长。但是,如果逻辑最终变得不可读,请考虑使用我建议的方法。
我可以访问 /v1/folder
但无法访问 /v1/folder/<folder-id>
。你能告诉我原因吗?在flask-request document中表示add_resource()
可以路由多个URI。但是我不能。也许我误解了什么。如果你找到线索,请告诉我。
from flask import request
from flask_restful import Resource, abort
class Folder(Resource):
def post(self, folder_id):
return { "message":"post with folder_id"}, 200
def post(self):
return { "message":"post without folder_id"}, 201
app = Flask(__name__)
.....
api_bp = Blueprint('api', __name__)
api = Api(api_bp, serve_challenge_on_401=True)
api.add_resource( Folder, '/v1/folder', '/v1/folder/<string:folder_id>')
app.register_blueprint(api_bp)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True )
错误消息是 "TypeError: post() got an unexpected keyword argument 'folder_id' "。怎么了?
Python 不支持 function/method 重载,因此您最后声明的 post
方法总是会被使用。相反,您应该使用 Python 提供的工具 - 参数的默认值。
我个人会做以下事情:
from flask import request
from flask_restful import Resource, abort
class Folder(Resource):
def post(self, folder_id=None):
if folder_id is None:
return self.__simple_post()
else:
return self.__parameter_post(folder_id)
def __parameter_post(self, folder_id):
return { "message":"post with folder_id"}, 200
def __simple_post(self):
return { "message":"post without folder_id"}, 201
app = Flask(__name__)
.....
api_bp = Blueprint('api', __name__)
api = Api(api_bp, serve_challenge_on_401=True)
api.add_resource( Folder, '/v1/folder', '/v1/folder/<string:folder_id>')
app.register_blueprint(api_bp)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True )
或者您可以处理 post
方法中的逻辑,前提是逻辑足够相似且不太长。但是,如果逻辑最终变得不可读,请考虑使用我建议的方法。