How to divide flask app into multiple py files?

后端 未结 5 763
一生所求
一生所求 2020-11-29 17:06

My flask application currently consists of a single test.py file with multiple routes and the main() route defined. Is there some way I could creat

5条回答
  •  渐次进展
    2020-11-29 17:37

    This task can be accomplished without blueprints and tricky imports using Centralized URL Map

    app.py

    import views
    from flask import Flask
    
    app = Flask(__name__)
    
    app.add_url_rule('/', view_func=views.index)
    app.add_url_rule('/other', view_func=views.other)
    
    if __name__ == '__main__':
        app.run(debug=True, use_reloader=True)
    

    views.py

    from flask import render_template
    
    def index():
        return render_template('index.html')
    
    def other():
        return render_template('other.html')
    

提交回复
热议问题