烧瓶模板未找到

sav*_*123 31 python jinja2 flask

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

jinja2.exceptions.TemplateNotFound

TemplateNotFound: template.html
Run Code Online (Sandbox Code Playgroud)

主要的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)
Run Code Online (Sandbox Code Playgroud)

我有以下文件结构:

flask_new_practice
|--template/
    |--template.html
|--run.py
Run Code Online (Sandbox Code Playgroud)

Jef*_*yll 79

默认情况下,Flask会查看templates应用程序根级别的文件夹.

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

template_folder - 包含应用程序应使用的模板的文件夹.默认为应用程序根路径中的"templates"文件夹.

所以你有一些选择,

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

    app = Flask(__name__, template_folder='template')
    
    Run Code Online (Sandbox Code Playgroud)

  • 这节省了我的时间..谢谢 (2认同)