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
Related
What would be the correct approach to create a "stats block" which could be used easily?
I've now created a simple page template and view function which returns a list of stats e.g. users and user count by calling User.objects.all().count()
How should this be implemented to be able to include these stats in any template? And perhaps with different styles such as list, inline, bootstrap panel etc?
If you want it in any template, you probably want to write your own custom template tag. This is the relevant Django documentation: https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#writing-custom-template-tags
Your tag will probably return minimalist data: just the result of queryset.count(). The template that you are rendering will place this number in the relevant html context.
I want to make a sidebar for my webapplication. It contains the following content:
A search bar
a list of tags
links to recent posts (with year and month)
I want to include this sidebar in every site/view. So my first guess is that it would belong to a layout.
But it's also dynamic and as far as i know layouts are static.
How do I avoid redundancy in my views/layouts and still have the sidebar on every site?
To have context data passed to multiple templates you have different options in django; You could either:
Make a Template Tag which can pull in the relevant data and render it and reuse it in every template you need to (or just insert it in a base template and use template inheritance).
Use a context processor: It will be called with every request and add data to every view's context that uses RequestContext.
Using Django's class based views you could have all your views inherit from a base view which adds data to your context.
If you need data from your database I would rather go with using a template tag than using a context processor as it will be called for every view.
For static content in your sidebar (e.g. search form), its straight forward template/html.
For the dynamic content like list of tags, recent posts:
Once in the template you have identified a elements (div or something else) to put this info, you can populate its content using either your custom template tag or having custom context processor.
In your case, if the content doesn't really depend upon request parameter or url, template tag would be better choice.
Reference Custom template tag Custom Context Processor
I'm working on my first Django project and have my templates setup with a base that all the others extend. In that base I want to have some user-specific navigation which means loading some values from the database to build the contents of a drop down menu. However I don't want to have to do this inside each view. Coming from Symfony2/Twig I would normally do this using a sub-request where I tell the template to render a view and that will use it's own template. Using syntax like:
{% render 'Bundle:Controller:action' with {} %}
How would I accomplish this same thing with Django? I've read over the docs a couple of times but can't find any way to do this.
You have two approaches:
(better)
- add the code to base.html (the one you're always extending) and only override it when you need to.
or
(worse)
- in every template use {% include %} to include your menus.html template.
Update: re-reading your question: you could modify the request in context-processor so your base.html would then have this information.
Custom template tags are what you want.
When particularly extend template and when to use include ? Is include of any use with content like user profile section (like about me in the corner of our site) ?
Extending allows you to replace blocks (e.g. "content") from a parent template instead of including parts to build the page (e.g. "header" and "footer"). This allows you to have a single template containing your complete layout and you only "insert" the content of the other template by replacing a block.
If the user profile is used on all pages, you'd probably want to put it in your base template which is extended by others or include it into the base template. If you wanted the user profile only on very few pages, you could also include it in those templates.
If the user profile is the same except on a few pages, put it in your base template inside a block which can then be replaced in those templates which want a different profile.
See about django template inheretance.
Extends sort of 'includes' the parent template and then can overwrite parts of it for different functionality.
Include does a simple include rendering a template in a current context.
extends creates "parent child relation". There is a chance of over-writing of parent functionality in case of extends. While include simply renders the html response.
Using % include ... with see docs allows to override variables from included page. So could not agree with muhammad-awais-bin-majid answer.
Suppose these two clauses just represent different ways of the pages constructing:
extends - to use like an external wrapper for the page,
include - to insert included page inside the current one.
Also one can use several extending pages just in chained nesting,
but including allow us to use several included pages one by one and not only in chained nesting.
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.