flask蓝图的使用

做了一个模板,放在 github 。

https://github.com/chichungceng/flask-blueprint-model

补充一个点,就是当蓝图模块的 views.py 代码中,如果视图函数的 return render_template(同名.html) 和主模块 main.py 代码中视图函数的return render_template(同名.html) ,html 文件名一样时。即使你访问蓝图模块的 html ,也会跳到主模块的 html !

解决办法:

官方github提供了解决方法:
在项目初始化时,设置判断即可:


def create_app(env=‘development‘):    
    """此处略去若干行"""        
    @app.before_request    
    def before_request():        
        if request.blueprint is not None:            
            bp = app.blueprints[request.blueprint]            
            if bp.jinja_loader is not None:                
                newsearchpath = bp.jinja_loader.searchpath + app.jinja_loader.searchpath
                app.jinja_loader.searchpath = newsearchpath

原文地址:https://www.cnblogs.com/chichung/p/9799955.html