[Flask] Blueprints templates

Laura Morales lauretas at mail.com
Sun Mar 18 15:54:37 EDT 2018


I've found this old issue that's basically the same thing https://github.com/pallets/flask/issues/266

I think it would be useful to have an option to trigger this behavior.
 
 

Sent: Sunday, March 18, 2018 at 7:44 PM
From: "Jerry Mccreary" <jerry.mccreary at mac.com>
To: "Laura Morales" <lauretas at mail.com>
Cc: flask at python.org
Subject: Re: [Flask] Blueprints templates

I don’t think it is a hack, as much as it is how the operating system/python works.
 
The Flask project has a main ‘templates’ folder. The convenience of blueprints it that the blueprint folder
can have the same structure as the main templates folder.  When a view is rendered with `render_template()`,
it will first look in the blueprint’s template folder for the specified template, if doesn’t find it, then it looks in the main template folder.
(If I’m wrong, someone please correct me).
 
This is similar scenario of how python packages are referenced in a project. Your Flask app will first look in the project folder for a reference module,
then, if not finding it, will look in the installed packages.
 
Flask is not going to assume that a blueprint’s templates are in folder that happens to have the same name as the blueprint.
(I’m not sure if that would be desirable to have as an automatic assumption)
 
See where I referenced setting the template_folder parameter here: http://flask.pocoo.org/docs/0.12/blueprints/#my-first-blueprint 

  

On Mar 18, 2018, at 2:29 PM, Laura Morales <lauretas at mail.com[mailto:lauretas at mail.com]> wrote: 
Yes I'd like to divide my websites into 2 modules (blueprints), developed by two separate groups. So, each blueprint will contain its own templates, views, static assets etc.
Right now I use "module-1/templates/module-1/template.html" but it looks a bit hackish. I was hoping there was a way to remove the need of the inner "module-1" folder, and have Flask instead automatically recognize that "module-1/template.html" is actually "template.html" inside "module-1/templates/".
 the blueprint using templates_folder attribute.
By using this parameter, you point to the blueprint’s template directory
once, and the associated views will look in that directory.
Does this solve the problem with different blueprints having a template with the same name?
 
 

Sent: Sunday, March 18, 2018 at 7:07 PM
From: "Jerry Mccreary" <jerry.mccreary at mac.com[mailto:jerry.mccreary at mac.com]>
To: "Laura Morales" <lauretas at mail.com[mailto:lauretas at mail.com]>
Cc: flask at python.org[mailto:flask at python.org]
Subject: Re: [Flask] Blueprints templates

I’m not sure if this answers your question:
 
The structure you reference is for project and maintenance organization, keeping blueprint assets together.
Blueprints can also go in the main templates folder at root of project.
 
Ex:
yourpackage/
blueprints/
admin/
__init__.py
models.py
views.py
 
templates/  (main templates folder)
admin/
index.html
blueprint2/
index.html 
blueprint3/
index.html

 
The subfolders are used because blueprints may have templates named the same.
For example, if all the templates were in a single directory, they would need to be name without conflict.
 
yourpackage/
blueprints/
etc.
 
templates/ (main templates folder)
admin_index.html
blueprint1_index.html
blueprint2_index.html
blueprint3_index.html
 
Also, when defining the a blueprint, the corresponding template can be defined when declaring the blueprint using templates_folder attribute.
By using this parameter, you point to the blueprint’s template directory once, and the associated views will look in that directory.


More information about the Flask mailing list