[Python] Flask "TemplateNotFound" Error for index.html Despite Correct Folder Structure

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I am building a web application using Flask for a school project. However, I’m encountering a recurring issue where Flask cannot find the index.html template. I’ve verified that the folder structure is correct, and the templates are in the appropriate folder.

    Here’s my folder structure:

    oshinosensei/
    ├── static/
    │ ├── Lorem.jpg
    │ ├── Ipsum.jpg
    │ ├── ...
    ├── templates/
    │ ├── index.html
    │ ├── result.html
    └── app.py


    Despite the correct setup, Flask is unable to find the index.html template when I try to run the app. After running python app.py, the server starts successfully, but the page fails to load due to this error:

    jinja2.exceptions.TemplateNotFound: index.html


    I have confirmed that index.html is in the templates folder, and I’ve printed the current working directory in my app.py file:

    import os
    print("Current working directory:", os.getcwd())


    The output confirms that the working directory is correct:

    Current working directory: C:\Users\남경천\oshinosensei


    import os
    from flask import Flask, render_template, request

    app = Flask(__name__, template_folder='templates')

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

    if __name__ == "__main__":
    app.run(debug=True)


    I’ve also tried adding the template_folder='templates' argument when initializing Flask, but the error persists.

    Any help or suggestions on how to resolve this issue would be greatly appreciated!

    Continue reading...

Compartilhe esta Página