Django

Code

Ticket #5124 (closed: fixed)

Opened 1 year ago

Last modified 10 months ago

{% block %} inheritance not working after a few levels of {% extends %} (includes minimal demo app)

Reported by: vdupras@goombah.com Assigned to: k0001
Milestone: Component: Template system
Version: SVN Keywords: extends block load templatetags
Cc: favo@exoweb.net Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

must_be_first.diff (1.6 kB) - added by k0001 on 12/01/07 02:26:37.
adds support for a new Node property "must_be_first"
contains_nontext.diff (0.6 kB) - added by AmanKow on 02/07/08 13:07:21.
contains_nontext_2.diff (0.7 kB) - added by AmanKow on 02/07/08 15:07:52.

Change History

08/09/07 10:24:27 changed by vdupras@goombah.com

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Why can't I attach my file to the ticket?

08/09/07 10:31:59 changed by vdupras@goombah.com

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:

http://s3.amazonaws.com:80/goombah/extends_bug.zip

08/09/07 11:35:49 changed by ubernostrum

You cannot attach .zip or .tar.gz files here; the preferred format is a single .diff.

09/15/07 16:46:32 changed by anonymous

  • owner changed from nobody to anonymous.
  • status changed from new to assigned.

09/15/07 16:46:53 changed by buriy

  • owner changed from anonymous to buriy.
  • status changed from assigned to new.

09/15/07 17:50:36 changed by buriy

  • status changed from new to 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.

09/15/07 19:24:12 changed by buriy

  • stage changed from Unreviewed to 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

Patches for both decisions are very small.

11/03/07 14:32:00 changed by buriy

  • stage changed from Design decision needed to 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.

12/01/07 02:26:37 changed by k0001

  • attachment must_be_first.diff added.

adds support for a new Node property "must_be_first"

12/01/07 02:33:22 changed by k0001

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 =)

12/01/07 02:35:58 changed by k0001

  • status changed from assigned to closed.
  • resolution set to fixed.

12/01/07 03:24:35 changed by SmileyChris

  • status changed from closed to reopened.
  • resolution deleted.
  • stage changed from Accepted to 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.

12/01/07 14:50:44 changed by buriy

  • owner changed from buriy to k0001.
  • status changed from reopened to new.

02/03/08 22:52:37 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7084]) Fixed #5124 -- Added a reasonable error when "extends" is not the first template tag. Patch from k0001.

Refs #6274.

02/07/08 13:07:21 changed by AmanKow

  • attachment contains_nontext.diff added.

02/07/08 13:08:29 changed by AmanKow

  • status changed from closed to reopened.
  • has_patch set to 1.
  • resolution deleted.

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.

02/07/08 15:07:37 changed by AmanKow

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.

02/07/08 15:07:52 changed by AmanKow

  • attachment contains_nontext_2.diff added.

02/07/08 16:40:34 changed by SmileyChris

  • status changed from reopened to closed.
  • resolution set to fixed.

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.


Add/Change #5124 ({% block %} inheritance not working after a few levels of {% extends %} (includes minimal demo app))




Change Properties
Action