Opened 16 years ago

Closed 16 years ago

#6295 closed (fixed)

forloop internals should create a single context dictionary, rather than recreating it on each iteration

Reported by: Ned Batchelder Owned by: nobody
Component: Template system Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In this django-users thread (http://groups.google.com/group/django-users/browse_thread/thread/3c79d7e1df438c68) I asked how to make an iffirst tag. My first attempt at the tag was to maintain state in the contextforloop dictionary. I found that it isn't not maintained between iterations. Malcolm chimed in with exactly the same intuition, and also found that it didn't work.

Looking at the code, there seems to be no good reason to re-make the dictionary each time.

This patch changes the code to keep the same dictionary for the life of the forloop, so that other tags can use the dictionary to maintain state.

I also added tests for forloop.first and forloop.last, which were never tested directly...

Attachments (1)

forloop.diff (3.0 KB ) - added by Ned Batchelder 16 years ago.
the patch!

Download all attachments as: .zip

Change History (2)

by Ned Batchelder, 16 years ago

Attachment: forloop.diff added

the patch!

comment:1 by Adrian Holovaty, 16 years ago

Resolution: fixed
Status: newclosed

(In [6981]) Fixed #6295 -- Made the {% for %} tag a bit more efficient by creating a single context dictionary rather than recreating it each time through the loop. Thanks, Ned Batchelder

Note: See TracTickets for help on using tickets.
Back to Top