python - 找不到 flask 模板

标签 python flask jinja2

从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

最佳答案

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

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

template_folder – the folder that contains the templates that should be used by the application. Defaults to 'templates' folder in the root path of the application.

所以你有一些选择,

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

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

关于python - 找不到 flask 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396064/

相关文章:

python - 使用 Jinja 按嵌套字典值过滤

google-app-engine - “ self ”未定义,jinja2,appengine

python - 使 df.apply 成为索引和列函数的最佳方法

python - 如何在 Python 字符串中包含转义字符

python - 更新变量的值?

python - 谷歌应用引擎和谷歌云 sql 上的 Flask SqlAlchemy

python - 使用cors的Azure Python Flask应用程序在部署时似乎没有安装requirements.txt?

python - 从源安装 setuptools "No module named numbers"错误

linux - Ansible - 如何在 lineinfile 模块中使用三元函数?

python - django 2 中的迁移错误;属性错误 : 'str' object has no attribute 'decode'