Python 找不到烧瓶模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33396064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:17:25  来源:igfitidea点击:

Flask Template Not found

pythonflaskjinja2

提问by saviour123

Implementing a simple static site from flask, but the browser says template not found, the shell returned 404

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

jinja2.exceptions.TemplateNotFound

TemplateNotFound: template.html

The main python code:

主要的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)

I have the following file structure:

我有以下文件结构:

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

采纳答案by Jeffrey Godwyll

By default, Flask looks in the templatesfolder in the root level of your app.

默认情况下,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.

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

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

So you have some options,

所以你有一些选择,

  1. rename templateto templates
  2. supply a template_folderparam to have your templatefolder recognised by the flask app:

    app = Flask(__name__, template_folder='template')
    
  1. 重命名templatetemplates
  2. 提供一个template_folder参数,让templateFlask 应用程序识别您的文件夹:

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