Code

Opened 6 years ago

Closed 6 years ago

#6295 closed (fixed)

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

Reported by: nedbatchelder 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:

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 nedbatchelder 6 years ago.
the patch!

Download all attachments as: .zip

Change History (2)

Changed 6 years ago by nedbatchelder

the patch!

comment:1 Changed 6 years ago by adrian

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed

(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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.