【问题标题】:Flask Template Not found [duplicate]找不到烧瓶模板[重复]
【发布时间】:2016-01-28 12:32:14
【问题描述】:

从flask实现一个简单的静态站点,但是浏览器说找不到模板,shell返回404

jinja2.exceptions.TemplateNotFound

TemplateNotFound: template.html

主要python代码:

from flask import Flask, render_template
app = Flask(__name__)


@app.route("/")
def template_test():
    return render_template('template.html', my_string="Wheeeee!", my_list=[0,1,2,3,4,5])

if __name__ == '__main__':
    app.run(debug=True)

我的文件结构如下:

flask_new_practice
|--template/
    |--template.html
|--run.py

【问题讨论】:

    标签: python flask jinja2


    【解决方案1】:

    默认情况下,Flask 会在您应用的根目录中查找 templates 文件夹。

    http://flask.pocoo.org/docs/0.10/api/

    template_folder – 包含模板的文件夹 由应用程序使用。默认为根目录中的“模板”文件夹 应用程序的路径。

    所以你有一些选择,

    1. template重命名为templates
    2. 提供template_folder 参数,让烧瓶应用程序识别您的template 文件夹:

      app = Flask(__name__, template_folder='template')
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多