-1

I'm currently building a flask app and created a project structure that is really organized. I want to render a template, that is located in a subdirectory but I can't figure out why it is not finding the HTML file. I tried many different paths and tried joining the path using the OS module but I couldn't get it to work. Thanks for helping me!


my-flask-app
   ├── app/
   │   └── website/
   |       |──templates/
   |       |  └──home.html #The html file I want to render
   |       └──views.py #The file where the template is rendered      
   └── main.py
from flask import Blueprint, render_template, request, flash
import os

views = Blueprint("views", __name__)

@views.route("/", methods=["GET", "POST"])
def home():
    return render_template("./website/templates/home.html")
Celltox
  • 127
  • 8

2 Answers2

0

The first thing you should do is check the working directory of your python app. You can do this by printing os.getcwd() in the environment.

Looking at your structure, try this:

return render_template("website/templates/home.html")

Do update the answer with the error.

rajat yadav
  • 374
  • 2
  • 7
0

The problem is that the function render_template does not take a path, it takes the name of the file in the templates folder.

So, you would instead pass in just home.html, instead of ./website/templates/home.html .

Like this:

from flask import Blueprint, render_template, request, flash
import os

views = Blueprint("views", __name__)

@views.route("/", methods=["GET", "POST"])
def home():
    return render_template("home.html")
HackDolphin
  • 166
  • 11