Python - Flask:找不到 render_template()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23435150/
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 02:58:23  来源:igfitidea点击:

Python - Flask: render_template() not found

pythonweb-applicationsflaskjinja2

提问by Mill

I'm new in flask. I have this code: Will you give me an advice what I'm doing wrong? Thanks

我是烧瓶新手。我有这个代码:你能给我一个建议我做错了什么吗?谢谢

from flask import Flask
from flask import request
from flask import render_template

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('my-form.html')

@app.route('/', methods=['POST'])
def my_form_post():

    text = request.form['text']
    processed_text = text.upper()
    return processed_text
app.debug = True
print("asdsa")
if __name__ == '__main__':
    app.run()

I have put file my-form.html into this folder: C:\Python33\Lib\site-packages\flask\testsuite\templates

我已将文件 my-form.html 放入此文件夹:C:\Python33\Lib\site-packages\flask\testsuite\templates

But when I refresh the 127.0.0.1:5000 I get jinja2.exceptions.TemplateNotFound:

但是当我刷新 127.0.0.1:5000 时,我得到 jinja2.exceptions.TemplateNotFound:

File "C:\Python33\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python33\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python33\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python33\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python33\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python33\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python33\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python33\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python33\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python33\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\workspace\ApproximateStringSearch\testest.py", line 9, in my_form
return render_template('my-form.html')
File "C:\Python33\lib\site-packages\flask\templating.py", line 127, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Python33\lib\site-packages\jinja2\environment.py", line 830, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Python33\lib\site-packages\jinja2\environment.py", line 791, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Python33\lib\site-packages\jinja2\environment.py", line 765, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Python33\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Python33\lib\site-packages\flask\templating.py", line 64, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: my-form.html

采纳答案by davidism

You put your template in the wrong place. From the Flask docs:

您将模板放在错误的位置。来自 Flask 文档:

Flask will look for templates in the templates folder. So if your application is a module, this folder is next to that module, if it's a package it's actually inside your package:

Flask 将在模板文件夹中寻找模板。所以如果你的应用程序是一个模块,这个文件夹就在那个模块旁边,如果它是一个包,它实际上在你的包中:

See the docs for more information: http://flask.pocoo.org/docs/quickstart/#rendering-templates

有关更多信息,请参阅文档:http: //flask.pocoo.org/docs/quickstart/#rendering-templates