8

I'm testing out Flask with blueprints. My app has two blueprints:

  1. base
  2. opinions

base/__init__.py

base = Blueprint('base', __name__, static_folder='static', template_folder='templates') 
#http://server.com/base

opinions/__init__.py

opinions = Blueprint('opinions', __name__, static_folder='static', template_folder='templates')
#http://server.com/opinions

__init__.py

app = Flask(__name__)
from app.base import views 
from app.base import base
app.register_blueprint(base, url_prefix='/base')

from app.opinions import views
from app.opinions import opinions
#app.register_blueprint(opinions, url_prefix='/opinions')  <-- Uncommenting this line causes issues

If I register only 1 of these blueprints, everything runs fine. However, if I register both blueprints, templates are always loaded from opinions. For example if I hit http://server.com/base , the index.html gets picked from opinions folder. Flask documentation does not mention anything about 'template_folder' namespace conflicts.

PS - I would like to know alternative ways of handling multiple blueprints. I'm not very comfortable importing views file from two different blueprints. Whats the better way to do this?

akaRem
  • 7,326
  • 4
  • 29
  • 43
Neo
  • 13,179
  • 18
  • 55
  • 80
  • Please include an example usage of the statics in your template and the generated html line. – Paolo Casciello Mar 05 '14 at 13:28
  • @PaoloCasciello - Your comment sure helped. It was not the static files, but the wrong templates which were getting picked. I have edited the question appropriately. – Neo Mar 05 '14 at 18:58
  • possible duplicate of [flask blueprint template folder](http://stackoverflow.com/questions/7974771/flask-blueprint-template-folder) – Neo Mar 06 '14 at 08:00

1 Answers1

9

Blueprint template directories are registered globally. They share one namespace so that your app can override the blueprint's template if necessary. This is mentioned briedly in the documentation.

Thus, you should not name your opinions' template index.html, but rather opinions/index.html. That makes for awkward paths at first glance (…/opinions/templates/opinions/…) but adds flexibility for customizing "canned" templates without changing the blueprint's contents.

Matthias Urlichs
  • 2,301
  • 19
  • 29