include template which extends something else

19 views
Skip to first unread message

svewa

unread,
Oct 28, 2015, 11:28:07 AM10/28/15
to Django users
Hi all,

sorry if it might be a stupid question for pro's but it seems I still miss a deeper knowledge of some design aspects. What I try to do is the following:

<ul>
{% for obj in objects %}
        <li>
            {% include 'item.html' with object=obj %}
        </li>
{% endfor %}
</ul>

this I need to to in three different apps with slightly varying appearance of item.html.

So my idea was to create a root_item.html in my root template folder and simply do in item.html the following::

{% extends "root_item.html" %}
{% block anyblock %}
{% endblock %}

I'am so far that I guess I know that this will not work. Could anyone explain to me the prefered way to achieve this? In words, I need a base template which extensions should be rendered as list items...

Sorry I'am shure it is easy but I am stuck  

Tim Graham

unread,
Oct 28, 2015, 11:39:13 AM10/28/15
to Django users
I think that's a limitation described in the {% include %} docs: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#include. See the "Note" box where it says:

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

----
It's difficult for me to make a suggestion about how to structure things without a more complete understanding of what you're trying to do.

Sven Wanner

unread,
Oct 28, 2015, 11:51:12 AM10/28/15
to django...@googlegroups.com
Thanks for your quick reply. Yes, this is also what I found, so I was quite shure that my code will never work. What I try to do is to render a list of database objects. But not only on one page but on different pages and with slightly different designs in each of the lists. So my plan was, to avoid writing individual templates for each list, to create one base template for the general appearance of such list items and only adapt blocks on each individual list.

Explained the other way around, how would I render a template.html which needs a database object  in a for loop and which extends a base_template html?

Hope I achieved to explain the problem well enough...

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SFh41AI0dcM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/af32f8fd-e83b-4767-8f37-eaccc1ec70b8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Oct 28, 2015, 11:56:47 AM10/28/15
to Django users
I don't think you can structure your templates that way. Maybe using {% if %} tags in your included template would be enough (instead of using {% extends %}).

Carl Meyer

unread,
Oct 28, 2015, 12:04:54 PM10/28/15
to django...@googlegroups.com
Hi Sven,

On 10/28/2015 09:50 AM, Sven Wanner wrote:
> Thanks for your quick reply. Yes, this is also what I found, so I was
> quite shure that my code will never work. What I try to do is to render
> a list of database objects. But not only on one page but on different
> pages and with slightly different designs in each of the lists. So my
> plan was, to avoid writing individual templates for each list, to create
> one base template for the general appearance of such list items and only
> adapt blocks on each individual list.
>
> Explained the other way around, how would I render a template.html which
> needs a database object in a for loop and which extends a base_template
> html?
>
> Hope I achieved to explain the problem well enough...

If I'm understanding right, I think blocks do work fine for your situation.

What you can't do, as Tim explained, is have blocks in an included
template interact with blocks in the including template. They are two
totally separate renders, with totally separate inheritance hierarchies.
But there's no problem with included templates having their own
independent inheritance hierarchy, with blocks.

So you can have an `item_base.html` that contains `{% block someblock
%}{% endblock %}`, and then define `item_foo.html` that `{% extends
"item_base.html" %}` and has `{% block someblock %}foo content{%
endblock %}`, and you can have a `main.html` that has `{% include
"item_foo.html" %}`, and all that should work fine -- the included
portion should display "foo content".

What you can't do is expect the `{% block someblock %}` in
`item_base.html` or `item_foo.html` to interact in any way with a `{%
block someblock %}` in `main.html` (or its parent). The included
template and the including template have separate and unrelated
inheritance and blocks.

Carl

signature.asc

Sven Wanner

unread,
Oct 28, 2015, 12:11:34 PM10/28/15
to django...@googlegroups.com
Ok, it's a pitty, but you're totally right. Using if in my template should be also fine. Not of that elegance I planned  ;) , but will definitely work, too.

Thank you 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SFh41AI0dcM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Carl Meyer

unread,
Oct 28, 2015, 12:14:00 PM10/28/15
to django...@googlegroups.com
On 10/28/2015 10:11 AM, Sven Wanner wrote:
> Ok, it's a pitty, but you're totally right. Using if in my template
> should be also fine. Not of that elegance I planned ;) , but will
> definitely work, too.

I'm not sure what you mean. I thought I just explained how you can use
`extends` in an included template -- I think that should work just fine
for your use case (and I just tested to verify that it does work).

Carl

signature.asc

Sven Wanner

unread,
Oct 28, 2015, 12:25:30 PM10/28/15
to django...@googlegroups.com
Yes, sorry I was confused... No I don't get what you mean. I think thats exactly what I tried to do. 

I have kind of a main.html doing that:

{% block my-wall %}
<div id="wall-container">
<ul id="wall-list">
        {% for obj in objects %}
<li>
                    {% include 'wall_item.html' with object=obj %}

</li>
{% endfor %}
</ul>
    </div>
{% endblock %}
wall_item.html looks like that:

{% extends "wallitem.html" %}

{% block wall_item_header %}
<p>something</p>
{% endblock %}
and wallitem.html like that:

<div class="wall-list-item">
<div class="userprofile-container">
{% block wall_item_header %}
{% endblock %}
</div>
</div>


Carl

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SFh41AI0dcM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Carl Meyer

unread,
Oct 28, 2015, 12:28:46 PM10/28/15
to django...@googlegroups.com
That template structure should work just fine. I just tested here with a
similar set of templates and it worked. So there's something else going
on. Can you be more specific about how it fails? Do you get an error?
Unexpected output in some way?

Carl

signature.asc

Sven Wanner

unread,
Oct 28, 2015, 12:34:26 PM10/28/15
to django...@googlegroups.com
Oh man sorry, there was something wrong with finding the template but many thanks for your advices. It helped me a lot even if the problem was blindness. But anyway, I am happy now. 

Best regards to you
Reply all
Reply to author
Forward
0 new messages