본문 바로가기
Etc/Error

[Error] jinja2.exceptions.TemplateNotFound:

by VAMOSSS 2023. 1. 7.
반응형

jinja2.exceptions.TemplateNotFound:

jinja2.exceptions.TemplateNotFound:

Flask 공식문서의 Quickstart - Rendering Templates를 보게 되면 다음과 같은 설명이 있다.

 

결론부터 말하자면 templates 폴더를 생성해 안에 적용하고 싶은 template을 넣으면 된다.

 

Generating HTML from within Python is not fun, and actually pretty cumbersome because you have to do the HTML escaping on your own to keep the application secure. Because of that Flask configures the Jinja2 template engine for you automatically.
To render a template you can use the render_template() method. All you have to do is provide the name of the template and the variables you want to pass to the template engine as keyword arguments. Here’s a simple example of how to render a template:
from flask import render_template

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)
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:

Case 1: a module:

/application.py
/templates
    /hello.html

Case 2: a package:

/application
    /__init__.py
    /templates
        /hello.html

 

나의 경우 index.html을 templates 폴더 안에 넣었더니 해결되었다.

(폴더 이름을 template으로 생성해서 오류가 났었다,,,)

Reference

https://flask.palletsprojects.com/en/0.12.x/quickstart/#rendering-templates

 

Quickstart — Flask Documentation (0.12.x)

For web applications it’s crucial to react to the data a client sends to the server. In Flask this information is provided by the global request object. If you have some experience with Python you might be wondering how that object can be global and how

flask.palletsprojects.com

반응형

댓글