【问题标题】:Flask Application Alternative File Structure (app.py, templates folder)Flask 应用程序替代文件结构(app.py,模板文件夹)
【发布时间】:2020-12-18 08:37:02
【问题描述】:

我正在构建一个简单的烧瓶应用程序,其中 文件结构

  • 项目/
    • app.py
    • 模板/
    • 静态/
      • css/
        • style.css
      • js/
        • code.js

使用这种结构,下面的代码完全可以正常工作。

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def root():
    return(render_template('html/index.html'))
if(__name__ == '__main__'):
    app.run(debug=True)

但我想将我的所有 python 代码(在本例中为 app.py)保存在一个名为 server/ 的包中,然后如果需要创建一些子该包内的包 (server/)。

这样,项目文件结构如下所示

  • 项目/
    • 服务器
      • app.py
    • 模板/
    • 静态/
      • css/
        • style.css
      • js/
        • code.js

但是有了这个文件结构,我想知道路由路径需要是什么,这样我就可以在 template/html 文件夹中链接我的 html 文件。

@app.route('/')
def root():
    return(render_template('html/index.html'))

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    初始化Flask对象时提供相对或绝对路径:

    app = Flask(__name__, template_folder="../templates")
    

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多