Python - Flask Default Route possible?

前端 未结 3 1210
情深已故
情深已故 2020-12-05 04:44

In Cherrypy it\'s possible to do this:

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

Is there a flask equivalent?

3条回答
  •  悲哀的现实
    2020-12-05 05:09

    There is a snippet on Flask's website about a 'catch-all' route for flask. You can find it here.

    Basically the decorator works by chaining two URL filters. The example on the page is:

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

    Which would give you:

    % 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
    

提交回复
热议问题