実行環境
Python 3.7.0
PyCharm Professional 2019.2 version
Jinja2==3.0.1
Flask==2.0.1
実現したい事
Python Flaskの勉強中なのですが,以下のコードをもちいてlocalhostでindex.htmlの内容を表示したいと考えております。
試したコード
python
1from flask import Flask, render_template 2app = Flask(__name__) 3 4@app.route("/") 5def index(): 6 return render_template('index.html') 7 8if __name__ == "__main__": 9 app.run(debug=True)
html
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6</head> 7<body> 8<h1>Hello world</h1> 9</body> 10</html>
フォルダ構造
flask-aap
|- app.py
|- templates
|-- index.html
Errorの内容
ブラウザ上に,以下のErrorが表示され,index.htmlが表示されません。
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: index.htmlTraceback (most recent call last)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2309, in call
return self.wsgi_app(environ, start_response)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File "C:\Users\nakajyu1\drml\backend\main.py", line 24, in index
return render_template("index.html")
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 134, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\ProgramData\Anaconda3\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:\ProgramData\Anaconda3\lib\site-packages\jinja2\environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\ProgramData\Anaconda3\lib\site-packages\jinja2\environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "C:\ProgramData\Anaconda3\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 58, in get_source
return self._get_source_fast(environment, template)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.htmlThe debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
teratail上や,色々なサイトを調べると,pythonプログラムと同じ階層にtemplatesフォルダを作成し,その中にindex.htmlを保存するとエラーが解消されるとありましたが,解決しませんでした。
ご教示頂けますと幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。