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

Flask 的 templates 目录不能自己指定吗?

  •  
  •   chaogg · 2016-07-03 15:27:11 +08:00 · 5553 次点击
    这是一个创建于 2826 天前的主题,其中的信息可能已经有所发展或是发生改变。

    文档中是这么说的: Flask 将会在 templates 文件夹中寻找模版。因此如果你的应用是个模块,这个文件夹在模块的旁边,如果它是一个包,那么这个文件夹在你的包里面:

    Case 1: 一个模块:

    /application.py
    /templates
        /hello.html
    

    Case 2: 一个包:

    /application
        /__init__.py
        /templates
            /hello.html
    

    这是不是规定死了模板的位置?我在代码中调用 render_template 发现自己指定模板目录是不行的

    4 条回复    2016-09-30 13:21:01 +08:00
    petelin
        1
    petelin  
       2016-07-03 16:14:04 +08:00 via Android
    创建 flask 对象时候,是需要传一个模块一般是__name__过去,你改下就行了,那个是被当作根地址。可以从这里入手。
    chaogg
        2
    chaogg  
    OP
       2016-07-03 16:53:07 +08:00
    @petelin 这倒是个思路。改了这个,会不会破坏 Flask 的其他依赖?
    Pythagodzilla
        3
    Pythagodzilla  
       2016-07-03 22:59:40 +08:00
    Flask 的 constructor 不是有个 template_folder 的参数么
    alphadog619
        4
    alphadog619  
       2016-09-30 13:21:01 +08:00
    可以 如果项目使用蓝图的话 main = Blueprint('main',__name__,template_folder='template'),就是指定模板文件夹为当前包中的 template
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2788 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:20 · PVG 20:20 · LAX 05:20 · JFK 08:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.