Flask函数有多个参数值

时间:2018-11-19 10:34:19

标签: python python-3.x flask

我有以下Python代码:

@reports_api.route('/reports/xlsx/organisations/<int:organisation_id>/', methods=['GET'])
@reconnect_to_db
@check_permissions(request, employee_constraints={}, client_user_constraints={}, in_args=True)
def get_organisation_containers_report_xlsx(organisation_id, employee_id):
    if request.method == 'GET':
        recipient = request.args.get('recipient')
        report_str_io = ExcelReportsManager.get_organisation_containers_report(organisation_id, employee_id, recipient == 'up')
    return flask.jsonify(**report_str_io), 200

当我使用该API的路由时,出现错误

TypeError: get_organisation_containers_report_xlsx() got multiple values for argument 'organisation_id'

有通向api的路径:

http://localhost:5000/reports/xlsx/organisations/1/?employee_id=2

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为第一个参数是烧瓶path方法中的route,将path添加到函数签名中作为第一个参数,并检查其是否固定。

答案 1 :(得分:0)

函数args中出错。需要使用request.args.get('employee_id')

代码将如下所示:

@reports_api.route('/reports/xlsx/organisations/<int:organisation_id>/', methods=['GET'])
@reconnect_to_db
@check_permissions(request, employee_constraints={}, client_user_constraints={}, in_args=True)
def get_organisation_containers_report_xlsx(organisation_id):
if request.method == 'GET':
    recipient = request.args.get('recipient')
    report_str_io = ExcelReportsManager.get_organisation_containers_report(organisation_id, request.args.get('employee_id'), recipient == 'up')
return flask.jsonify(**report_str_io), 200

当我们向api的路径中发送args时,它起作用:

http://localhost:5000/reports/xlsx/organisations/1/?employee_id=2