Opened 10 years ago

Closed 10 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: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


In this django-users thread ( 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 10 years ago.
the patch!

Download all attachments as: .zip

Change History (2)

Changed 10 years ago by Ned Batchelder

Attachment: forloop.diff added

the patch!

comment:1 Changed 10 years ago by Adrian Holovaty

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