Capture arbitrary path in Flask route

谁说我不能喝 提交于 2019-11-26 22:23:25
moodh

Use the path converter to capture arbitrary length paths: <path:path> will capture a path and pass it to the path argument. The default converter captures a single string but stops at slashes, which is why your first url matched but the second didn't.

If you also want to match the root directory (a leading slash and empty path), you can add another rule that sets a default value for the path argument.

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_dir(path):
    return path

There are other built-in converters such as int and float, and it's possible to write your own as well for more complex cases.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!