Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all routes for Flask [duplicate]

Tags:

python

flask

I'm using Flask with React. I'd like to create a catch all route similar to something like this (although this doesn't work):

@app.route('*') def get():     return render_template('index.html') 

Since my app will use React and it's using the index.html to mount my React components, I'd like for every route request to point to my index.html page in templates. Is there a way to do this (or is using a redirect the best way)?

like image 206
hidace Avatar asked Aug 20 '17 01:08

hidace


People also ask

How do you get all the routes on a Flask?

To get list of all routes defined in the Python Flask app, we can use the app. url_map property. to print the list of routes in our app with print(app. url_map) .

Can you have multiple app routes in Flask?

In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page/response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function.

Can you use multiple decorators to route URLs to a function in Flask?

The decorator syntax is just a little syntactic sugar. This code block shows an example using our custom decorator and the @login_required decorator from the Flask-Login extension. We can use multiple decorators by stacking them.

What is dynamic route in Flask?

Dynamic Routing: It is the process of getting dynamic data(variable names) in the URL and then using it.


1 Answers

You can follow this guideline: https://flask.palletsprojects.com/en/1.1.x/api/#url-route-registrations

from flask import Flask app = Flask(__name__)  @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path):     return 'You want path: %s' % path  if __name__ == '__main__':     app.run() 
like image 111
I. Helmot Avatar answered Sep 19 '22 19:09

I. Helmot