flask blueprint template folder

synergetic :

My flask app layout is:

myapp/
    run.py
    admin/
        __init__.py
        views.py
        pages/
            index.html
    main/
        __init__.py
        views.py
        pages/
            index.html

_init_.py files are empty. admin/views.py content is:

from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='pages')

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

main/views.py is similar to admin/views.py:

from flask import Blueprint, render_template
main = Blueprint('main', __name__, template_folder='pages')

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

run.py is:

from flask import Flask
from admin.views import admin
from main.views import main

app = Flask(__name__)
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(main, url_prefix='/main')

print app.url_map

app.run()

Now, if I access http://127.0.0.1:5000/admin/, it correctly displays admin/index.html. However, http://127.0.0.1:5000/main/ shows still admin/index.html instead of main/index.html. I checked app.url_map:

<Rule 'admin' (HEAD, OPTIONS, GET) -> admin.index,
<Rule 'main' (HEAD, OPTIONS, GET) -> main.index,

Also, I verified that index function in main/views.py is called as expected. If I rename main/index.html to something different then it works. So, without renaming, how can achieve that 1http://127.0.0.1:5000/main/1 shows main/index.html?

linqq :

As of Flask 0.8, blueprints add the specified template_folder to the app's searchpath, rather than treating each of the directories as separate entities. This means that if you have two templates with the same filename, the first one found in the searchpath is the one used. This is admittedly confusing, and is poorly documented at this time (see this bug). It seems that you weren't the only one that was confused by this behavior.

The design reason for this behavior is so that blueprint templates can be easily overriden from the main app's templates, which are first-in-line in Flask's template searchpath.

Two options come to mind.

  • Rename each of the index.html files to be unique (e.g. admin.html and main.html).
  • In each of the template folders, put each of the templates in a subdirectory of the blueprint folder and then call the template using that subdirectory. Your admin template, for example, would be yourapp/admin/pages/admin/index.html, and then called from within the blueprint as render_template('admin/index.html').

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling method from Flask Blueprint

From Dev

Flask Blueprint can't find static folder

From Dev

Flask: Template in Blueprint Inherit from Template in App?

From Dev

Should templates be in blueprint template folders or the global template folder?

From Dev

Flask - A blueprint's name collision occurred

From Dev

How to init restless as flask blueprint

From Dev

Flask Session not persistant in blueprint

From Dev

Flask: serve template in another folder (other than 'templates/' directory)

From Dev

Overwrite route in flask blueprint

From Dev

Flask Blueprint 404

From Dev

How to run Flask app by flask run with blueprint template

From Dev

Cannot show image from STATIC_FOLDER in Flask template

From Dev

Flask - render template in other Blueprint without redirect

From Dev

Python Flask serving static files from root static folder rather than blueprint's static folder

From Dev

Flask Blueprint structure

From Dev

Flask: get current blueprint webroot

From Dev

Extends a Flask template outside the Module Folder

From Dev

render_template() cannot find template located in blueprint folder

From Dev

Flask - Forms - Blueprint

From Dev

Flask Template Folder Not Found

From Dev

Query while declaring Flask Blueprint

From Dev

can i make a link to an html file that is not the template folder in flask

From Dev

how to use flask render_template API to return multiple images for a HTML template in the template folder?

From Dev

Running a Flask blueprint from the program

From Dev

How can I locate my static folder in flask with a divisional blueprint?

From Dev

Flask blueprint and registering path

From Dev

Blueprint error in Flask: NameError: name of blueprint is not defined

From Dev

Flask Blueprint Subdomain Not Working GAE

From Dev

Inject dependencies in Flask blueprint

Related Related

  1. 1

    Calling method from Flask Blueprint

  2. 2

    Flask Blueprint can't find static folder

  3. 3

    Flask: Template in Blueprint Inherit from Template in App?

  4. 4

    Should templates be in blueprint template folders or the global template folder?

  5. 5

    Flask - A blueprint's name collision occurred

  6. 6

    How to init restless as flask blueprint

  7. 7

    Flask Session not persistant in blueprint

  8. 8

    Flask: serve template in another folder (other than 'templates/' directory)

  9. 9

    Overwrite route in flask blueprint

  10. 10

    Flask Blueprint 404

  11. 11

    How to run Flask app by flask run with blueprint template

  12. 12

    Cannot show image from STATIC_FOLDER in Flask template

  13. 13

    Flask - render template in other Blueprint without redirect

  14. 14

    Python Flask serving static files from root static folder rather than blueprint's static folder

  15. 15

    Flask Blueprint structure

  16. 16

    Flask: get current blueprint webroot

  17. 17

    Extends a Flask template outside the Module Folder

  18. 18

    render_template() cannot find template located in blueprint folder

  19. 19

    Flask - Forms - Blueprint

  20. 20

    Flask Template Folder Not Found

  21. 21

    Query while declaring Flask Blueprint

  22. 22

    can i make a link to an html file that is not the template folder in flask

  23. 23

    how to use flask render_template API to return multiple images for a HTML template in the template folder?

  24. 24

    Running a Flask blueprint from the program

  25. 25

    How can I locate my static folder in flask with a divisional blueprint?

  26. 26

    Flask blueprint and registering path

  27. 27

    Blueprint error in Flask: NameError: name of blueprint is not defined

  28. 28

    Flask Blueprint Subdomain Not Working GAE

  29. 29

    Inject dependencies in Flask blueprint

HotTag

Archive