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.
Related
I have a flask-admin app with quite a number of custom templates. I want to add an html block at the top of every page in the app. The obvious solution would be to add it to every template (and don't forget to add it to any new custom templates) but I hope there might be a better solution, to specify the html I want in a single place and have all future new templates automatically have it.
To simply insert HTML snippets into your templates you could use
{% include 'template_file.html' %}
To automatically get all new templates to use the HTML block try the jinja2 template inheritance which all of your templates would need to inherit from. Docs
I want to load a list notifications for almost all the web application I am building. Should I do in every view function or View Class?
No, you should not, Django provides several mechanisms to take care of that kind of issues.
Context processors are functions that are called each time a template is rendered and which output populates that template's context. They allow you to have the same variables available everywhere in your templates.
Custom template tags and especially inclusion tags allow you to output content anywhere in a template by just calling them, which is as simple as {% my_tag_name %}.
These two mechanism coupled with template inheritance and inclusion allow you to not repeat code that is shared across many pages.
What is the difference between Django's built-in include tag and custom inclusion tags?
I have read the documentation, and both seem to to achieve the same goal: render a template passing it a context or variable.
They serve different purposes. The include tag simply includes the content from an existing template in its entirety and unmodified. A custom inclusion tag passes the context to a function which can contain logic to manipulate the context before passing it to a template.
For example, perhaps I have a panel that will be shown on multiple pages. The panel's template requires a few specific queries to be passed to it through the context. The pages that contain the panel don't require those context variables for anything else. If I include the panel template with the include tag, I would have to write those queries in every view that contains the panel and pass them as context variables.
Alternatively, I could write a custom inclusion tag that contains the queries and passes them to the panel's template. By using the custom inclusion tag I wouldn't need to repeat the code to produce its context in every view that contains the panel. My views would contain less code and would be less cluttered with context variables only used by the panel.
Although you are correct in the sense that a custom inclusion tag that simply passes on the context unmanipulated would be the same as the include tag.
Need to separate templates to smaller files? Use include tag (for readability and maintainability and DRY)
Need to include more code before rendering the template? Use inclusion tags (fetch more data, add some business logic.. it is really like another small url-less view. it is like a template function).
In principle, the point made by dgel's and YardenST's answers is correct. Additionally, a look into django's code gives a good insight on how these two options are compared in performance.
When using the default template loaders, there is absolutely no difference between the two. Both eventually make a call to the InclusionTag render() function, which in turn makes a call to template Loader get_contents() that opens the template file from filesystem. render() only caches the file in case it is used in a template for loop.
As a side note, a difference in performance would be possible by using the django.template.loaders.cached.Loader.
Last, regarding dgel's suggestion to use the inclusion tag for common context across different views: it is very much possible to avoid the small extra overhead of rendering an inclusion template, when the html markup is in a single base template that spans across many views, by using a ContextMixin. This is a quite common scenario for rendering eg. a main menu in a base template.
Just recently faced this question as I was trying to find which route is best to take - include vs inclusion tag - when there's no real extra logic that might go into an inclusion tag.
And I choose the inclusion tag for the following reasons:
more compact and readable markup in templates
<!-- include -->
{% include "path/to/funky.html" with arg1=arg1 arg2=arg2 %}
vs
<!-- inclusion tag -->
{% funky arg1 arg2 %}
easier to maintain code, if you ever had to add custom logic to the tag then it's easy to add
it enforces you to use template variables that are scoped properly, so the inclusion tag cannot inherit a variable from the parent view, makes it more resilient against weird bugs
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.