When a Flask route returns 404 even though you are sure you wrote that route, stop guessing URLs in the browser. The flask routes command prints every registered route — endpoint, methods and path — in a single table. Just point FLASK_APP at the right app file and run the command, and you can immediately see which routes exist, which are missing and which have the wrong method.
The Problem
The bigger a Flask app gets, the harder it is to remember every route. Blueprints add a url_prefix, routes nest, some routes only accept POST — a single wrong detail and the browser returns 404 or 405 without explaining why. What many people do is print app.url_map to the terminal or try each URL one by one: slow, and easy to miss a route sitting in a blueprint that was never registered.
print(app.url_map)# Map([<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,# <Rule '/' (HEAD, OPTIONS, GET) -> index>,# <Rule '/bai-viet/<post_id>' (HEAD, OPTIONS, GET) -> post_detail>,# <Rule '/admin/dashboard' (HEAD, OPTIONS, GET) -> admin.dashboard>])
The Solution
The flask routes command has been built into Flask since 2.2, with nothing extra to install. First set the FLASK_APP environment variable to point at the file holding the app — or use the --app flag — then run the command:
export FLASK_APP=app.pyflask routes
The result is a tidy table:
Endpoint Methods Rule--------------- ------- -----------------------admin.dashboard GET /admin/dashboardindex GET /post_detail GET /bai-viet/<int:post_id>static GET /static/<path:filename>
The table has three columns: Endpoint is the view function name (with a blueprint it looks like blueprint_name.function_name), Methods lists the HTTP methods the route accepts, and Rule is the real path Flask matches — including the blueprint’s url_prefix. By default the table is sorted by endpoint. To see the order Flask matches routes when two rules overlap, use flask routes --sort match, because the first matching rule wins. To also see the HEAD and OPTIONS methods Flask adds automatically, use the --all-methods flag.
Apply It to Your Project
Make flask routes part of your debugging habit: every time you hit a 404, run the command before touching any code and check three things in order. One, is the route in the table at all — if not, the blueprint was probably never registered with register_blueprint. Two, is the path correct, including the prefix — for example a dashboard registered in the admin blueprint with the /admin prefix has the correct URL /admin/dashboard, while /dashboard returns 404. Three, does the method match — POSTing to a route that only accepts GET returns 405, not 404. These three checks solve most route errors without ever opening the debugger.
Related Posts
- Flask: Separate Templates and Static Files per Blueprint — Stop Your templates/ Folder from Ballooning in 5 Minutes
- Flask: Stop Circular Imports — Use current_app Instead of Importing app Directly in 5 Minutes
- Flask: Stop Hardcoding URLs — Use url_for So Route Changes Never Break Links
FAQ
Does flask routes need the server running?
No. The command only reads the app, prints the table and exits — it opens no port and runs no server. It needs FLASK_APP to point at the right app file — if the file name is wrong, Flask immediately reports a “Could not import module” error.
Why doesn’t a route inside a Blueprint show up in the table?
Because that blueprint has not been registered with app.register_blueprint(). Flask only lists routes registered on the app; a blueprint that was imported but never registered has routes that do not exist yet.
Why does the Methods column only show GET when the route also accepts POST?
Because the @app.route decorator only accepts GET by default. To accept POST you must declare it explicitly with methods=["GET", "POST"] in the decorator — the flask routes table reflects exactly what was registered, so it is the fastest way to spot a forgotten method declaration.
How is flask routes different from printing app.url_map?
app.url_map prints one long, hard-to-read Map object, while flask routes presents the same data as a table with columns, sortable by endpoint, method, rule or match order. With an app that has dozens of routes, the table is far easier to scan.
💬 Have you tried this in your own project? If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.
Sources
- Flask documentation — “Command Line Interface” (Routes section): https://flask.palletsprojects.com/en/stable/cli/
- Flask documentation — “Quickstart: Routing”: https://flask.palletsprojects.com/en/stable/quickstart/
