为什么args.get()在Python中不返回任何值?

2024-05-23 14:10:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获取一个名为'indicator_id'的查询参数,但由于某些原因,它始终是由args.get()返回的类型None


parser = reqparse.RequestParser()
parser.add_argument('indicator_id', type = str, required=True)

@api.route('/collections?indicator_id=<indicator_id>', methods = ['GET', 'DELETE'])
@api.expect(parser)
class Collection(Resource):

    @api.response(201, 'Created Successfully')
    @api.response(404, 'Indicator not found')
    def get (self, indicator_id):

        args = parser.parse_args()
        indicator_id = args.get('indicator_id')
        print(str(indicator_id))


Tags: noneaddapiidparser类型参数get
1条回答
网友
1楼 · 发布于 2024-05-23 14:10:33

您不需要在api.route中描述您的查询参数,只需在视图函数中将它们作为request.args或dict(request.args)检索即可

如果您希望它成为url的一部分,您需要将其描述为动态路径:

@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    pass

相关问题 更多 >