Django templates: enable and extend

I would like to provide the same content inside two different base files.

So, I am trying to do this:

page1.html:

{% extends "base1.html" %} {% include "commondata.html" %} 

page2.html:

 {% extends "base2.html" %} {% include "commondata.html" %} 

The problem is that I cannot use both extensions and include. Is there any way to do this? And if not, how can I accomplish the above?

commondata.html overrides the block that is specified in both base1.html and base2.html

The goal of this is to provide the same page in pdf and html format, where the formatting is slightly different. The above question, although it simplifies what I'm trying to do, if I can get an answer to this question, it will solve my problem.

+91
python django django-templates
Sep 11 '09 at 4:08
source share
7 answers

When you use the extends template tag, you say that the current template extends another - it is a child template, depending on the parent template. Django will consider your child template and use its contents to populate the parent.

Everything you want to use in the child template must be within the blocks that Django uses to populate the parent. If you want to use the include statement in this child template, you must put it in a block so that Django can understand it. Otherwise, it just does not make sense, and Django does not know what to do with it.

The Django documentation has some really good examples of using blocks to replace blocks in a parent template.

https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance

+93
Sep 11 '09 at 4:13
source share

From the Django docs:

The include tag should be considered as the implementation of "rendering this subtopic and including HTML", and not as "parsing this subtopic and including its contents as if it were part of the parent." This means that there is no common state between the included templates - each of them is a completely independent rendering process.

So, Django does not capture any blocks from your commondata.html file and does not know what to do with the displayed external html blocks.

+71
Nov 08
source share

This should do the trick for you: put the include tag inside the block section.

page1.html:

 {% extends "base1.html" %} {% block foo %} {% include "commondata.html" %} {% endblock %} 

page2.html:

 {% extends "base2.html" %} {% block bar %} {% include "commondata.html" %} {% endblock %} 
+13
May 10 '17 at 14:29
source share

More info on why it doesn't work for me if it helps future people:

The reason it didn't work is because {% include%} in django doesn't like special characters like the fantastic apostrophe. The template data I was trying to include was inserted from a word. I had to manually remove all these special characters, and then it turned on successfully.

+11
Sep 11 '09 at 5:21
source share

You cannot pull blocks from an included file into a child template to override parent template blocks. However, you can specify the parent in the variable and specify the base pattern in context.

From the documentation :

{% extends variable%} uses the value of the variable. If a variable evaluates to a string, Django will use that string as the name of the parent template. If the variable is evaluated by the Template object, Django will use this object as the parent template.

Instead of the separate "page1.html" and "page2.html" put {% extends base_template %} at the top of "commondata.html". And then, in your opinion, define base_template as "base1.html" or "base2.html".

+3
Apr 19 '13 at 18:57
source share

Added for links to future people who find this via google. You might want to see the {% overextend%} tag provided by the mezzanine library for such cases.

+2
Dec 15
source share

Edit December 10, 2015 . As stated in the comments, ssi has been deprecated since version 1.8. According to the documentation:

This tag is deprecated and will be removed in Django 1.10. Use the include tag instead.




In my opinion, the correct (best) answer to this question is the question from podshumok , as it explains why include include behavior when used with inheritance.

However, I was somewhat surprised that no one mentioned the ssi tag provided by the Django template system, specially designed for inline, including an external piece of text. Here, inline means that the external text will not be interpreted, analyzed or interpolated, but simply β€œcopied” inside the calling template.

Please refer to the documentation for more information (be sure to check the appropriate version of Django in the selector at the bottom right of the page).

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#ssi

From the documentation:

 ssi Outputs the contents of a given file into the page. Like a simple include tag, {% ssi %} includes the contents of another file – which must be specified using an absolute path – in the current page 

Also beware of the security implications of this method, as well as the required ALLOWED_INCLUDE_ROOTS define, which must be added to your settings files.

+1
May 13 '14 at 8:24
source share



All Articles