简体   繁体   中英

Context variables not available in django template after first pass through a for loop

I am passing a list from a view to a template via a context variable and everything is working fine inside a for loop. Now I have a requirement that is best satisfied by wrapping the original for loop in an outer loop. What I've discovered is that the original list disappears after a single pass through the inner loop and is not available to be repeated in subsequent iterations of the parent loop. I'm sure that this is a best practice with regard to memory management but it makes it almost impossible for me to implement the solution I had in mind. Is there a way to override this behavior so that the lists in the context are preserved in subsequent iterations? Thanks in advance.

FWIW, I did find a better solution, so this feature of Django prevented me from implementing a bad one. The thing that saved me was the {% ifchanged %} tag, which allowed me to identify when to take the action that I needed to take on the first (and only) pass through the for loop. Cool!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM