Python

에러 | jinja2.exceptions.TemplateNotFound: 해결 방법

ㅈㅣ니 2023. 10. 26.

html 파일을 찾지 못하고 이런 에러가 발생할 때 원인은

[ flaskEx03.py ]

from flask import Flask, render_template

# [주의] 실행파일과 같은 층위에 template 폴더를 만들고 그 폴더에 html 파일을 배치해둔다!

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('demo.html')

if __name__ == '__main__':
    app.run()

폴더명이 잘 못 되었을 가능성이 있다.

render_template()는 루트 경로에서 templates 라는 폴더안에서 view 파일을 찾는데,

폴더명을 templates 라고 지정하지 않았을 때 오류가 나기도 한다.

이렇게 바꿔주면 다시 실행했을 때 html 파일이 잘 나오는 걸 확인할 수 있다.

반응형

'Python' 카테고리의 다른 글

Pycharm에서 Flask 설치  (0) 2023.10.25