首页 > 如何获得 Flask 项目所有的视图极其对应的 route

如何获得 Flask 项目所有的视图极其对应的 route

项目的视图函数都是用 @app.route('...') 修饰访问路径的,有没有办法把所有的视图极其 GET, POST 之类的方法路径全列出来呢?


看文档:http://dormousehole.readthedocs.org/en/latest/api.html#application-object
可以用url_map来获取。
例如app = Flask(__name__),则是app.url_map就可以列出路径
项目路径
simplebbs
   ├── app
   ├── config.py
   ├── manage.py
   ├── requirements.txt
   └── venv
在simplebbs路径下运行python命令

(venv) C:\zdata\code\simplebbs>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.160
tel)] on win32
Type "help", "copyright", "credits" or "license" for more informatio
>>> import app
>>> app.app.url_map
Map([<Rule '/topics/create' (GET, OPTIONS, HEAD, POST) -> topics.cre
 <Rule '/topics/latest' (GET, OPTIONS, HEAD) -> topics.latest>,
 <Rule '/users/setting' (GET, OPTIONS, HEAD, POST) -> users.setting>
 <Rule '/auth/signout' (GET, OPTIONS, HEAD) -> auth.signout>,
 <Rule '/auth/signin' (GET, OPTIONS, HEAD, POST) -> auth.signin>,
 <Rule '/auth/signup' (GET, OPTIONS, HEAD, POST) -> auth.signup>,
 <Rule '/auth/reset' (GET, OPTIONS, HEAD, POST) -> auth.reset>,
 <Rule '/auth/find' (GET, OPTIONS, HEAD, POST) -> auth.find>,
 <Rule '/topics/' (GET, OPTIONS, HEAD) -> topics.index>,
 <Rule '/' (GET, OPTIONS, HEAD) -> frontend.index>,
 <Rule '/topics/<id>/reply' (OPTIONS, POST) -> topics.reply>,
 <Rule '/topics/<id>' (GET, OPTIONS, HEAD) -> topics.topic>,
 <Rule '/static/<filename>' (GET, OPTIONS, HEAD) -> static>,
 <Rule '/users/<username>' (GET, OPTIONS, HEAD) -> users.index>])
>>>
【热门文章】
【热门文章】