Python - Flask 默认路由可能吗?

新手上路,请多包涵

在 Cherrypy 中可以这样做:

 @cherrypy.expose
def default(self, url, *suburl, **kwarg):
    pass

有等价的烧瓶吗?

原文由 John Jiang 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 904
2 个回答

Flask 的网站上有一段关于 Flask 的“包罗万象”路线的片段。 你可以在这里找到它

基本上,装饰器通过链接两个 URL 过滤器来工作。页面上的例子是:

 @app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

这会给你:

 % curl 127.0.0.1:5000          # Matches the first rule
You want path:
% curl 127.0.0.1:5000/foo/bar  # Matches the second rule
You want path: foo/bar

原文由 alemangui 发布,翻译遵循 CC BY-SA 4.0 许可协议

@app.errorhandler(404)
def handle_404(e):
    # handle all other routes here
    return 'Not Found, but we HANDLED IT

原文由 David 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏