flask blueprint template folder

后端 未结 5 1989
傲寒
傲寒 2020-11-30 18:32

My flask app layout is:

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


        
5条回答
  •  我在风中等你
    2020-11-30 19:10

    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').

提交回复
热议问题