Python - Flask:找不到render_template() [英] Python - Flask: render_template() not found

查看:1452
本文介绍了Python - Flask:找不到render_template()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的瓶子。我有这样的代码:
你会给我一个建议,我做错了什么?谢谢

 从瓶子导入Flask 
从瓶子导入请求$ b $从瓶子导入render_template

app = Flask(__ name__)

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

@ app.route('/',methods = ['POST'])
def my_form_post():
$ b $ text = request.form ['text ']
processed_text = text.upper()
return processed_text
app.debug = True
print(asdsa)
if __name__ =='__main__':
app.run()

我把文件my-form.html放到这个文件夹:C:\Python33\Lib\site-packages\flask\testsuite\template



但是当我刷新127.0.0.1:5000时,我得到

 app.py,第1836行,在__call__ 
中返回self.wsgi_app(environ,start_response)
文件C:\Python33\lib\site-packages\flask\app.py,行1820,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件C:\Python33\lib\site-packages\flask\app.py,第1403行,在handle_exception
reraise(exc_type,exc_value,tb)
文件C:\Python33\lib\site-packages\flask\_compat.py,第33行,在重新
提高价值
文件C:\Python33\lib \site-packages\flask\app.py,行1817,在wsgi_app
response = self.full_dispatch_request()
文件C:\Python33\lib\site-packages \flask\app.py,在1477行,在full_dispatch_request
rv = self.handle_user_exception(e)
文件C:\Python33\lib\site-packages\flask\\ \\ app.py,第1381行,在handle_user_exception
reraise(exc_type,exc_value,tb)
文件C:\Python33\lib\site-packages\flask\_compat.py 第33行,在rerais e
提高价值
在full_dispatch_request
文件C:\Python33\lib\site-packages\flask\app.py,1475行rv = self.dispatch_request ()
文件C:\Python33\lib\site-packages\flask\app.py,第1461行,在dispatch_request
返回self.view_functions [rule.endpoint]( ** req.view_args)
文件D:\workspace\ApproximateStringSearch\testest.py,第9行,在my_form
返回render_template('my-form.html')
文件C:\Python33\lib\site-packages\flask\templating.py,行127,在render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
文件C:\Python33\lib\site-packages\jinja2\environment.py,第830行,在get_or_select_template
中返回self.get_template(template_name_or_list,parent,globals)
文件C:\Python33\lib\site-packages\jinja2\environment.py,第791行,​​在get_templa te
返回self._load_template(name,self.make_globals(globals))
文件C:\Python33\lib\site-packages\jinja2\environment.py,第765行,在_load_template
template = self.loader.load(self,name,globals)
文件C:\Python33\lib\site-packages\jinja2\loaders.py,第113行,加载
来源,文件名,uptodate = self.get_source(环境,名称)
文件C:\Python33\lib\site-packages\flask\template.py ,第64行,在get_source中
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound:my-form.html


解决方案

你把你的模板放在错误的地方。从Flask文档:


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

该文档的更多信息: http://flask.pocoo.org/docs/quickstart/ #rendering-templates


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

But when I refresh the 127.0.0.1:5000 I get 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

解决方案

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

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:

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

这篇关于Python - Flask:找不到render_template()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆