Flask Jinja Template Condition Based on Blueprint View - flask

I haven't seen mention of this in the documentation, but is it possible to have a variable in a template based on what blueprint view is being used? This relates to my base.html and in one blueprint I want to have a different navbar, but I don't want to need to set this for every route/view in that blueprint specifically, nor have multiple base.html for each blueprint either.

The request object contains a property blueprint, which contains the registered name of the current blueprint. You should be able to render different code based on that.

Related

Use the modified admin template only for one Image app

Django 3.0.6
For one model I need a modified admin site template.
Namely, I want to modify this template:
admin/includes/fieldset.html
I have copied the fieldset.html from Django package directory and placed it like this:
/my_project/image/templates/admin/includes/fieldset.html
Here image is my application. It is this application that needs a modified admin template.
The problem is that all other models also get this template. And the used template filters don't receive necessary params and explode.
Documentation: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#set-up-your-projects-admin-template-directories
Well, I got confused and fail to organize the necessary directories structure.
How can I use the modified template only for one Image app?
You have a couple of issues. Firstly, only the following templates can be overridden per-app or per-model:
actions.html
app_index.html
change_form.html
change_form_object_tools.html
change_list.html
change_list_object_tools.html
change_list_results.html
date_hierarchy.html
delete_confirmation.html
object_history.html
pagination.html
popup_response.html
prepopulated_fields_js.html
search_form.html
submit_line.html
fieldset.html isn't in there, so you'll need to see if the including template(s) is in this list, and if it is, replace this template and use it to include your own version of fieldset.html.
Also, your location (slightly modified since can't override included templates per-app): /my_project/image/templates/admin/template.html isn't quite right. This will overwrite that template for every app. To do it per-app, you need a further subdirectory under admin:
/my_project/image/templates/admin/image/template.html
The reason for this is that templates don't really care about which app they're in, so the fact that the template lives in your image app doesn't mean anything to Django, the convention of putting them in your app's sub-directory is solely to avoid overriding templates that you don't intend to.

Django - call view method when including template

Is it possible in Django to have a view method associated to template, so when I {% include %} a child template in my parent template, the child template can call it's view method and get some data? Or the only way is to have a single view method associated to url that collects data for every template?
The question is not about the template inheritance. It's more about the scenario when there are a few templates included on a single page.
By default there is no way to automatically call view in django when you are using include tag in templates.
If you want just to call some python code from inside the template, check out custom template tags. For your use case best option will be custom inclusion tag

How to manage Django reusable app templates that extend common base templates?

I have a project with some components that should probably be separated out into reusable apps. However, all of the templates extend a couple of base templates for the entire site. Should I include copies of the base template in each app's template directory? What is the best way to structure template directories/inheritance?
For the purposes of your app, base.html or whatever the base template is called, should contain the entire HTML document so that it can function on its own. If end-users want to override the template, they can add their own copy to templates/yourappname/base.html and do whatever they want inside that file, including extend their main site template.

include views vs render templates in grails

What is the technical differences between render a template and include a view in grails?
You generally render a template (from a controller) as part of an AJAX request when you're updating only part of a page. I don't know what you mean by 'include a view'. If you mean render a template from a GSP then it's generally just a convenient way of reusing pieces of your presentation within a view.
So if I have a template that contains a login form, I may want to include that template in several different layouts/pages.
If I have a template that contains search results I may want to render the template from a controller in response to an AJAX request that occurs when the user scrolls to the end of my current set of results or the user updates some search criteria.

Django: How to show data record into Templates without render from Views?

I have a file called header.html and it is included by base.html. In header.html, I have a list of Categories, which are stored in the database. Now, I want to give that list to header.html. The problem is that no function is known to render the data into this file. So how do I do now. Heartfelt thanks!
You need a template tag - specifically, an inclusion tag. This will render a template with a custom context, in your case the list of categories.
If you want to make variables available in all templates without specifically passing them from a view you can use a Template Context Processor
That will populate your RequestContext, available in the template.
I also suggest you look at Template Inheritence to build the relationships between templates instead of including one within the other.