Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Flask Default Route possible?

In Cherrypy it's possible to do this:

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

Is there a flask equivalent?

like image 720
John Jiang Avatar asked Dec 03 '12 06:12

John Jiang


People also ask

What is the default route request in Flask?

By default, a route only answers to GET requests. You can use the methods argument of the route() decorator to handle different HTTP methods. If GET is present, Flask automatically adds support for the HEAD method and handles HEAD requests according to the HTTP RFC.

How does routing work in Flask?

Routing in FlaskThe route() decorator in Flask is used to bind an URL to a function. As a result when the URL is mentioned in the browser, the function is executed to give the result. Here, URL '/hello' rule is bound to the hello_world() function.

How do I create a route in Flask?

Adding routes is quite easy, all we need to do is use the Python decorator again followed by a new function. An 'about' route is shown highlighted here. After adding this code, the development server expectedly notices the change and reloads the server for us.


1 Answers

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('/<path:path>') 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 
like image 125
alemangui Avatar answered Sep 29 '22 12:09

alemangui