V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cuijiudai
V2EX  ›  Flask

flask Blueprint 的不解!

  •  
  •   cuijiudai · 2013-09-05 16:33:07 +08:00 · 9231 次点击
    这是一个创建于 3891 天前的主题,其中的信息可能已经有所发展或是发生改变。
    正在学flask,遇到问题:同时有多个 Blueprint 的时候,最后的一个Blueprint 指定的 template_folder 会覆盖其他的 Blueprint 的template_folder !!!不能每个 Blueprint 指定单独的 template_folder 吗?感觉找个设计对于各个 Blueprint 的独立开发相当不爽啊!
    6 条回复    2018-08-31 15:42:28 +08:00
    pengphy
        1
    pengphy  
       2013-09-05 17:30:28 +08:00
    额 明显可以的。。。

    看docs:http://flask.pocoo.org/docs/blueprints/
    simple_page = Blueprint('simple_page', __name__,
    template_folder='templates')
    cuijiudai
        2
    cuijiudai  
    OP
       2013-09-05 17:33:37 +08:00
    定义两个Blueprint ,
    simple_page = Blueprint('simple_page', __name__,
    template_folder='templates')

    page2 = Blueprint('page2', __name__, template_folder='templates', static_folder='static')

    #注册

    app.register_blueprint(simple_page, url_prefix='/pages')

    app.register_blueprint(page2, url_prefix='/page2')


    #这时候 simple_page 的 template_folder 会使用 page2 的 template_folder !!!而不是simple_page = Blueprint('simple_page', __name__,
    template_folder='templates')指定的!
    cuijiudai
        3
    cuijiudai  
    OP
       2013-09-05 17:34:03 +08:00
    @pengphy
    定义两个Blueprint ,
    simple_page = Blueprint('simple_page', __name__,
    template_folder='templates')

    page2 = Blueprint('page2', __name__, template_folder='templates', static_folder='static')

    #注册

    app.register_blueprint(simple_page, url_prefix='/pages')

    app.register_blueprint(page2, url_prefix='/page2')


    #这时候 simple_page 的 template_folder 会使用 page2 的 template_folder !!!而不是simple_page = Blueprint('simple_page', __name__,
    template_folder='templates')指定的!
    pengphy
        4
    pengphy  
       2013-09-05 17:36:50 +08:00
    cuijiudai
        5
    cuijiudai  
    OP
       2013-09-05 18:15:03 +08:00
    @pengphy 我是官方的例子改的,没看出来,能加我qq,帮我看下例子吗 QQ:461774563
    rleon
        6
    rleon  
       2018-08-31 15:42:28 +08:00
    其实官方文档有说明 http://flask.pocoo.org/docs/1.0/blueprints/

    多个 blueprint 的 template_folder 参数,会添加到系统的搜索路径下。这样,后面的 blueprint 路径会在前面。如果,多个 blueprint 都用类似 render_template("pages/index.html") ,则优先使用后面添加的 blueprint 下的 template_folder 路径。

    官方建议是,使用用如 render_template("bp1/index.html") ,render_folder("bp2/index.html"),
    并且放在路径 bp1/templates/bp1/index.html , bp2/templates/bp2/index.html 类似的结构下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:14 · PVG 07:14 · LAX 16:14 · JFK 19:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.