Opened 17 years ago
Closed 17 years ago
#5124 closed (fixed)
{% block %} inheritance not working after a few levels of {% extends %} (includes minimal demo app)
Reported by: | Owned by: | k0001 | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | extends block load templatetags | |
Cc: | favo@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a very, very, *very* strange and obscur bug. When I encountered it, I thought it was a simple {% extends %} and {% block %} misbehavior, but when I tried to build a minimal app to reproduce it, I couldn't. In fact, it has to do with extends, block *and* load (for templatetags).
I can't explain the bug, but please, someone, look at the demo app. "template6" has a block containing "THIS IS WHAT SHOULD BE OUTPUT BUT ISN'T". When you run the app, this text is not output.
If you remove the "{% load mytags %}" line from template5.html, oops! the text will be there.
*OR*
If you add "{% block css %}{{ block.super }}{% endblock %}" in template5.html, oops! the text is back again.
Sorry for not having simplified the demo further, I've been going crazy to try to reproduce the bug so I simply copy/pasted my template hierarchy and replaced text with dummy text.
Attachments (3)
Change History (19)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Oh well, it's definetly a buggy day for Django, when I try to attach a file I get a pgsql error. Well, until I can attach my file, you can get it there:
comment:3 by , 17 years ago
You cannot attach .zip
or .tar.gz
files here; the preferred format is a single .diff
.
comment:4 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:6 by , 17 years ago
Status: | new → assigned |
---|
Found the problem, it's related to the following code in source:django/trunk/django/template/loader_tags.py#L64:
class ExtendsNode(Node): ... def render(self, context): compiled_parent = self.get_parent(context) parent_is_child = isinstance(compiled_parent.nodelist[0], ExtendsNode) parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
parent_is_child is set to False, when nodelist[0] node is {% load "..." %} block, and mystics occurred.
I think correct solution should search for existance of ExtendsNode in nodelist, instead of picking up nodelist[0].
Continuing investigations to be sure my proposed change won't break anything.
comment:7 by , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
Core devs, please decide what to do:
- throw error that ExtendsNode should always be the first node (i think there was one)
or
- search ExtendsNode through blocks
Patches for both decisions are very small.
comment:8 by , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
Decision by Malcolm:
throw error that ExtendsNode? should always be the first node (i think there was one)
Problem:
I can't find good enough place to put it.
by , 17 years ago
Attachment: | must_be_first.diff added |
---|
adds support for a new Node property "must_be_first"
comment:9 by , 17 years ago
This patch adds support for a new Node property "must_be_first", which when set in a Node subclass (like ExtendsNode) means that that node should be the first one on the NodeList, if it isnt' it raises a TemplateSyntaxError when trying to add that node into the nodelist.
Thanks SmileyChris for your help on this =)
comment:10 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:11 by , 17 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
Triage Stage: | Accepted → Ready for checkin |
Thanks for the patch, k0001, but it's not fixed until it is applied.
I will however promote it to ready for checkin, because it does everything that Malcolm wants.
comment:12 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | reopened → new |
comment:13 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
by , 17 years ago
Attachment: | contains_nontext.diff added |
---|
comment:14 by , 17 years ago
Has patch: | set |
---|---|
Resolution: | fixed |
Status: | closed → reopened |
In 7089, a 'contains_nontext' attribute was added to the NodeList class as a class attribute, it needs to be an attribute of a NodeList instance.
comment:15 by , 17 years ago
Whoops! Sorry, I knocked that first diff out without thinking, of course when I ran my server, it failed. The correct diff is attached as contains_nontext_2.diff. Again, my appologies.
by , 17 years ago
Attachment: | contains_nontext_2.diff added |
---|
comment:16 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Thanks AmanKow for the patch, but in the future, please open a new ticket for a new bug rather than re-opening this ticket (you can always reference it). In fact, I'll close this one again -- could you please open a new ticket for this and reattach your patch to that one?
Also, you haven't actually explained the problem. It may help to add some tests showing the failing behavior.
Why can't I attach my file to the ticket?