1

I have a Flask project, FlaskUserAuthentication which has a package with same name(FlaskUserAuthentication) and under that package there are two more packages namely API and Site. The following is the structure-

enter image description here

Both the __init__.py files under the API and Site packages are empty.

The following is the code from __init__.py file under main FlaskUserAuthentication package, .

from flask import Flask
from API.routes import api
from Site.routes import site

app = Flask(__name__)

app.register_blueprint(api)
app.register_blueprint(site)

and the run.py has following-

from FlaskUserAuthentication import app

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

However, when I enter http://127.0.0.1:5000/index after running the server, I get following error-

jinja2.exceptions.TemplateNotFound: index.html

When clearly I have the index.html file under Site=>templates=>Site folder. The following is my Site=>routes.py code-

from flask import Blueprint, render_template

site = Blueprint('Site', __name__, template_folder='templates') # updated


@site.route('/index')
def index():
    return render_template('index.html')


@site.route('/login')
def login():
    return render_template('login.html')

Can anyone please help.

P.S.: I have updated my solution and the question accordingly after some suggestions. Still same issue.

3
  • Be careful with your folder naming - there's a mismatch between Site and site. It's OK on a case insensitive filesystem, but you'll hit problems if you every try to run your code on a case sensitive filesystem (e.g. on Linux).
    – Alasdair
    Nov 19, 2019 at 11:55
  • To debug the problem, set EXPLAIN_TEMPLATE_LOADING, i.e. app.config['EXPLAIN_TEMPLATE_LOADING'] = True.
    – Alasdair
    Nov 19, 2019 at 12:59
  • I ran into the problem where is I had missed the last s in the name of template. The folder name where we store the flask template is strictly should be templates as per the flask documentation.
    – Dev
    Jun 26, 2022 at 12:08

2 Answers 2

3

It looks as if you need to expose the templates by setting template_folder.

site = Blueprint('site', __name__, template_folder='templates')

See the blueprints docs for more info. In particular you might want to consider creating the template at Site/templates/Site/index.html and using render_template('Site/index.html'), so that the template can't be accidentally overriden by another index.html in the app's templates directory.

2
  • I followed your suggestion but no luck. I added another folder named 'Site' under the templates folder and also updated the Blueprint instance creation. I updated my question with current screen shot.
    – Natasha
    Nov 19, 2019 at 12:19
  • 1
    You changed the layout but didn't switch to render_template('Site/index.html') at the same time.
    – Alasdair
    Nov 19, 2019 at 12:57
0

if your folder structure is Site=>templates=>Site then

@site.route('/index')
def index():
    return render_template('Site/index.html') #add Site folder name before index.html
1
  • Thank you but it was mentioned earlier by the previous author. You should not answer with the already suggested solution.
    – Natasha
    Nov 20, 2019 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.