79đ
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.
- [Django]-Module "django.core.context_processors" does not define a "auth" callable request processor
12đ
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.
- [Django]-Setting default value for Foreign Key attribute in Django
- [Django]-Get the latest record with filter in Django
- [Django]-Generating a Random Hex Color in Python
5đ
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.
- [Django]-Django Rest Framework â Get related model field in serializer
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
1đ
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.
- [Django]-Aggregating save()s in Django?
- [Django]-Django â after login, redirect user to his custom page â> mysite.com/username
- [Django]-Should I avoid multi-table (concrete) inheritance in Django by any means?
0đ
-
can extend one template, then the child template can override {% block %} which defines in the parent template.
-
must be the 1st template tag in a template which means only one
{% extends %}
is allowed to use in one template.
So basically, you should put the regular layout components like {% block title %}
, {% block header %}
, {% block navigation-bar %}
, {% block main %}
and {% block footer %}
used on many pages to the foundation templates like base.html
, then extend base.html
with {% extend %}
in the main templates like index.html
.
- can inject(put) multiple templates to one template and the same template is injectable multiple times to one template.
So basically, you should put the irregular layout components used on a few pages to the main templates like index.html
except the foundation templates like base.html
.
*Lastly, I think that these Django admin templates are the good examples which use {% extends %}
, {% include %}
and {% block %}
.
- [Django]-Django package to generate random alphanumeric string
- [Django]-How can I run a celery periodic task from the shell manually?
- [Django]-Django request to find previous referrer
0đ
include: Copy (probably boilerplate) html from one page into another.
- For example, letâs say you have a navbar that appears on every page, but you donât want to retype that code over and over. Wouldnât it be nice if you could just code that navbar once and then include it into every page that needs a navbar? include makes that possible.
- Create a tiny file called nav.html with only your
navigation code inside and then include that nav.html file into
every page that needs a navbar like so:{% include 'nav.html' %}
extends: Wrap the substantive content of one page inside a generic template.
-
For example, letâs say you layout a webpage with all the generic stuff that will appear on each page: a header, a footer, etc⊠Letâs call this genericPage.html You can then make bunch of smaller html excerpts with just substantive content: home.html, about.html, contact.html, etcâŠ
-
You want to wrap the content of each substantive page inside that
generic wrapper. To do that, you can extend each of those content pages by wrapping them inside the main template by putting this code{% extends 'genericPage.html' %}
at the top of each page.
- [Django]-Get current user in Model Serializer
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-Adding new custom permissions in Django
0đ
{% extends âbase.htmlâ %} is used to create child templates for maintaining structure and content inheritance. {% include âbase.htmlâ %} is used to insert content from one template into another without altering the structure.
include will not change the overall structure of template(layout) where on the other hand extend will change the overall structure of the layout.
Depending upon your requirement you can choose what is more suitable for your work
- [Django]-Django â "Incorrect type. Expected pk value, received str" error
- [Django]-Django get the static files URL in view
- [Django]-Get current user in Model Serializer