Blueprint template folder in flask not working as expected

Viewed 1028

This is my project structure:

├── run.py
└── test_blueprint
    ├── __init__.py
    ├── __init__.pyc
    ├── mainsite
    │   ├── controllers
    │   │   ├── controllers.py
    │   │   ├── controllers.pyc
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── static
    │   │   ├── css
    │   │   │   ├── bootstrap.min.css
    │   │   │   └── signin.css
    │   │   └── js
    │   └── templates
    │       └── mainsite
    │           └── homepage.html
    ├── static
    │   ├── css
    │   │   ├── bootstrap.min.css
    │   │   └── signin.css
    │   └── js
    ├── templates
    │   └── mainsite
    │       └── index1.html
    └── usermanagement
        ├── controllers
        │   ├── controllers.py
        │   ├── controllers.pyc
        │   ├── __init__.py
        │   └── __init__.pyc
        ├── __init__.py
        ├── __init__.pyc
        ├── static
        └── templates

Here I have two blueprints, mainsite and usermanagement. I have registered them in file __init__.py under the test_blueprint folder which is the main folder (one below the blueprint_project folder which has run.py).

__init__ file under test_blueprint

from flask import Flask
app = Flask(__name__)
from test_blueprint.mainsite.controllers.controllers import mod 
from test_blueprint.usermanagement.controllers.controllers import mod 

app.register_blueprint(mainsite.controllers.controllers.mod, url_prefix="/")
app.register_blueprint(usermanagement.controllers.controllers.mod, url_prefix="/usermanagement")

Now post that I have under each blueprint folder I have created __init__.py in which I have defined Blueprints along with template folder. However, it does not take the template from there. It keeps on throwing error 'template not found'. So, I have created template folder under test_blueprint folder. It perfectly picks index.html from templates/mainsite folder.
Also, if I provide /home/user/flaskenv/blueprint_project/... templates folder which is an absolute path for my template under blueprint folder, it works fine.

Not sure if the bug is in Flask. I saw a video on YouTube and expected my flask project to behave the same. Unfortunately it didn't.

This is what my controller.py/views.py file looks like for mainsite:

from flask import Blueprint, render_template, url_for

mod = Blueprint('mainsite', __name__, static_folder='static',template_folder='templates')

@mod.route("/")
def home():
    return render_template('mainsite/homepage.html')

Now if I used os.path.abspath to locate the template folder under the blueprint folder, and then passed that path as variable to template_folder, it works fine.

Now my expectation as per flask documentation is that just mentioning template_folder='templates' should automatically locate templates under the blueprint folder or it entirely defeats the purpose.

1 Answers

I solved by renaming the html file by unique name

Related