2017-11-03 2 views
-1

Ich mache eine Website für ein Projekt, aber ich bekomme diesen Fehler immer wieder.flask: jinja2.exceptions.TemplateNotFound Fehler

jinja2.exceptions.TemplateNotFound: home.html

dann bekomme ich diese:

File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1997, in __call__ 
return self.wsgi_app(environ, start_response) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app 
response = self.handle_exception(e) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception 
reraise(exc_type, exc_value, tb) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise 
raise value 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app 
response = self.full_dispatch_request() 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request 
rv = self.handle_user_exception(e) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception 
reraise(exc_type, exc_value, tb) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise 
raise value 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request 
rv = self.dispatch_request() 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request 
return self.view_functions[rule.endpoint](**req.view_args) 
File "C:\Users\J\Desktop\hello.py", line 5, in home 
return render_template('home.html') 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 133, in render_template 
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list), 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template 
return self.get_template(template_name_or_list, parent, globals) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 830, in get_template 
return self._load_template(name, self.make_globals(globals)) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 804, in _load_template 
template = self.loader.load(self, name, globals) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\loaders.py", line 113, in load 
source, filename, uptodate = self.get_source(environment, name) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 57, in get_source 
return self._get_source_fast(environment, template) 
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 85, in _get_source_fast 
raise TemplateNotFound(template) 

hier auch mein Code

from flask import Flask, render_template 
app = Flask(__name__) 
@app.route('/') 
def home(): 
    return render_template('home.html') 
@app.route('/about/') 
def about(): 
    return render_template('about.html') 
if __name__ == '__main__': 
    app.run(debug=True) 
+2

Wo sind Ihre Vorlagen? Wenn Sie sich den [Schnellstart] (http://flask.pocoo.org/docs/0.12/quickstart/) ansehen, wird darauf hingewiesen, dass Flask im Vorlagenordner nach Vorlagen sucht. Sind Ihre Vorlagen dort? –

Antwort

1

Scheint, wie Sie Ihre HTML-Dateien sind nicht in der dasselbe Verzeichnis. Versuchen Sie, die Datei zu lesen und zu sehen, ob Sie sie öffnen können.

with open('home.html', 'r') as html_file: 
    html= html_file.read() 

oder versuchen, diese Struktur

import os.path 
os.path.isfile('home.html') 

Ihres Verzeichnisses zu tun wie dies

/home.py 
/templates 
    /home.html 
+0

Ich habe das versucht, aber ich bekomme immer noch den gleichen Fehler – vegasnoob

+0

Paul Rooney's Antwort ist richtig. Wie ist Ihre Struktur? –

2

Sie benötigen eine Vorlage in HTML-Dateien in einem Unterverzeichnis templates genannt angeordnet sind, um sicherzustellen, aussehen sollte.

Wenn sie dort befinden, wird alles funktionieren.

Eine einfache Erläuterung zu render_template finden Sie in der flask quickstart.

+0

Alle meine HTML-Dateien sind im Ordner Vorlagen und es funktioniert immer noch nicht. Irgendwelche anderen Vorschläge? – vegasnoob

+0

Wenn Ihre Anwendung als Paket strukturiert ist, erwartet sie, dass sich die Vorlagen in einem Verzeichnis 'templates' unter dem Modul root befinden. Könnte das das Problem sein? –

Verwandte Themen