web-dev-qa-db-fra.com

Python - Flacon: render_template () introuvable

Je suis nouveau en flacon. J'ai ce code: me donnerez-vous un conseil sur ce que je fais mal? Merci

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()

J'ai mis le fichier my-form.html dans ce dossier: C:\Python33\Lib\site-packages\flask\testsuite\templates

Mais quand je rafraîchis le 127.0.0.1:5000, j'obtiens 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
13
Mill

Vous placez votre modèle au mauvais endroit. À partir des documents Flask:

Flask recherchera les modèles dans le dossier des modèles. Donc, si votre application est un module, ce dossier est à côté de ce module, s'il s'agit d'un package, il se trouve en fait dans votre package:

Voir les documents pour plus d'informations: http://flask.pocoo.org/docs/quickstart/#rendering-templates

26
davidism