I have a complex Flask-based web app. There are lots of separate files with view functions. Their URLs are defined with the @app.route('/...')
decorator. Is there a way to get a list of all the routes that have been declared throughout my app? Perhaps there is some method I can call on the app
object?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
All the routes for an application are stored on
app.url_map
which is an instance ofwerkzeug.routing.Map
. You can iterate over theRule
instances by using theiter_rules
method:See Display links to new webpages created for a bit more information.
If you need to access the view functions themselves, then instead of
app.url_map
, useapp.view_functions
.Example script:
Output from running the script above:
(Note the inclusion of the "static" route, which is created automatically by Flask.)
inside your flask app do:
I just met the same question. Those solution above is too complex. Just open a new shell under your project:
The first 'app' is my project script: app.py, another is my web's name.
(this solution is for tinny web with a little route)
Apparently, since version 0.11, Flask has a built-in CLI. One of the built-in commands lists the routes:
Similar to Jonathan's answer I opted to do this instead. I don't see the point of using url_for as it will break if your arguments are not string e.g. float