应用文件为: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中,也可以这么干。
哈哈,终于又写了一篇。懒病总是需要克服!