![]() Obviously, the namespaces of both will conflict over the same directory names. Return FileSystemLoader(os.path.join(self.root_path,Īh! So now we see. """The Jinja loader for this package bound object. Tracing that chain to the very end, here is definition of jinja_loader (in flask/helpers.py, _PackageBoundObject, the base class of both the Flask application and Blueprints): def jinja_loader(self): Failing that, it checks all application blueprints (in order of registration, AFAIK) for blueprint.jinja_loader in an attempt to locate the file. Also, note that this is the same execution path that takes.ĭispatchingJinjaLoader does two things: First it checks if the app's global loader, which is app.jinja_loader can locate the file. If all goes according to plan, that chain will resolve in finding a file and will return its contents (and some other data). So when you call render_template in your app, it finds or creates the Jinja2 environment, sets up all those goodies, and calls get_template on it, which in turn calls get_source inside of the DispatchingJinjaLoader, which tries a few strategies described later. It also contains a reference to a template loader, in this case the aforementioned and auto-instantiated DispatchingJinjaLoader. ![]() The environment contains all of those context processors (e.g., so you can do var|tojson in a template), helper functions ( url_for, etc) and variables ( g, session, app). Take a look at flask/app.py to see how they reference those.) (You can specify your own Jinja2 options, by the way, by overriding app.jinja_options - but bear in mind that you'll lose two extensions which Flask includes by default - autoescape and with - unless you specify them yourself. These are created by Flask, with sensible defaults, automatically. There are two components you need to understand: Patching Flask's loading strategy requires some knowledge that doesn't seem to be documented, as well as a good understanding of Jinja2. This is where lack of documentation comes in. So, what is recommended is that one "override the jinja_loader function" instead. ![]() And if you screw your own loader up, it'll be able to lean on default, sane functionality. The reason that this is discouraged is that the DispatchingJinjaLoader has enough flexiblity to support the injection of custom loaders. One can override create_global_jinja_loader in a subclassed Flask application (which returns a DispatchingJinjaLoader defined in flask/templating.py). In addition to linqq's good suggestions above, you can also override the default functionality if needed. The blueprint as render_template('admin/index.html'). Your admin template, for example, would be yourapp/admin/pages/admin/index.html, and then called from within Templates in a subdirectory of the blueprint folder and then call In each of the template folders, put each of the.Rename each of the index.html files to be unique (e.g.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. It seems that you weren't the only one that was confused by this behavior. This is admittedly confusing, and is poorly documented at this time (see this bug). This means that if you have two templates with the same filename, the first one found in the searchpath is the one used. Renaming, how can achieve that 1 shows main/index.html?Īs 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. If I rename main/index.html to something different then it works. I checked app.url_map: admin.index,Īlso, I verified that index function in main/views.py is called as expected. However, shows still admin/index.html instead of main/index.html. Now, if I access, it correctly displays admin/index.html. Main = Blueprint('main', _name_, index():Īpp.register_blueprint(admin, url_prefix='/admin')Īpp.register_blueprint(main, url_prefix='/main') Main/views.py is similar to admin/views.py: from flask import Blueprint, render_template ![]() admin/views.py content is: from flask import Blueprint, render_templateĪdmin = Blueprint('admin', _name_, index():
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |