Wednesday, October 28, 2015

Re: include template which extends something else

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBCAAGBQJWMPH6AAoJEC0ft5FqUuEh13UP/izpT9EuzQXqE/jR8UmQafFI
lA584/+vbzx20IQixPJRF/7mtB49GXi/xeNWfy9SVla1zKzYvgS7sfm+xrVZNB1N
Dq7+rm9ZnGdCulvPTzyYSzt8l6EPBoS7Ri1jYupXdNL6hdBBpQ6WkZivGLYhWMtg
5MswOXAsBCwtuV0pfFawOjPShhL82MjqULZvITbXsLz6pilItSuayNtX9h3toB0e
ah+dq8/IBc/QhQKf3cfRvh+RcANanY9uAWmwiqHP7EibyeUYgJgDt6eAimjINysO
ORktYQDly5VbSlBRz3I4g9mtzHHcDq0OHK4o6hvpuuQfYpNyV+6Hq/RMAraYPAm4
oVzrXuKHjjF/3HspU2+YY/kQ5gibm9JwXmwsuMFyDF83VRZ3y//qWC+90PzQsDjM
TZNJnqgk6IxGf04es3xJCW2/GD5UOilhshI6s15uQ0HHp4J6vEoxeozsH1HHK88Q
B2dpELPeHgLHu78FcrBEYLjSbyUaaqki+/PeVgyytKPn7bP9YZKYxKHQ4oNwVocz
Hl1odMq6ABZ20ieuU5nFPqaT3POJbYc9S502OYMrzO/6+zZbgHLW76UmQFW08T2i
ZlBw5EGZIIcot0mdynyex6oY++VFv+7lNi6knMSmcu2xgLcvWmlmIWXez7Sp3+Bu
AneWVHSExZkjCiFUE9vN
=birf
-----END PGP SIGNATURE-----
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/5630F1F6.5090100%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment