应用文件为: app.py
- from flask import Flask
- app = Flask(__name__)
- @app.route("/")
- def hello():
- return "Hello World!"
- if __name__ == '__main__':
- app.run()
如果不想在这个文件中添加新路由, 怎么办?
有个直观的办法, 新建 test.py
- def add_new_routes(app):
- @app.route("/test")
- def test():
- return 'test'
然后在 app.py 中添加调用,
- from test import add_new_routes
- add_new_routes(app)
前面的办法缩进有点多啊! 换个方法, 将 test.py 的代码改为:
- def hello():
- return 'hello world'
- def add_new_routes(app):
- app.add_url_rule('/hello', view_func=hello)
同理, 在 Blueprint 中, 也可以这么干.
哈哈, 终于又写了一篇. 懒病总是需要克服!
来源: http://www.bubuko.com/infodetail-2733333.html