Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:18:06+00:00 2026-05-16T06:18:06+00:00

I was having trouble trying to iterate on a template on two dimensions at

  • 0

I was having trouble trying to iterate on a template on two dimensions at the same time.

The basic situation is explained here:

http://www.djangobook.com/en/2.0/chapter04/ ( in the apples, bananas indices example )

>>> from django.template import Template, Context
>>> t = Template('Item 2 is {{ items.2 }}.')
>>> c = Context({'items': ['apples', 'bananas', 'carrots']})
>>> t.render(c)
u'Item 2 is carrots.'

If I wanted to iterate from 1 to 3 on this with the variable “fruitstep”, I cannot do it in a template:

{{ items.fruitstep }} fails and considering long dot chains, this concept would lead to massive iteration requirements on the template. But I couldn’t find a standard way of doing it and I’m not sure it’s good template practice.

So, I created a Template filter:

@register.filter
def key2value(collection,key):
    try:
        return collection[unicode(key)]  # It seems that my collection 
                                         # keys are in unicode...
    except:
        return ""

This seems like an extremely powerful filter. It started off being a very specific tag, but I couldn’t think of a reason not to make it completely generic.

I’m wondering if there is a standard way to do this and I’ve reinvented the wheel, or if this code could do something that can compromise the system.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:18:07+00:00Added an answer on May 16, 2026 at 6:18 am

    No, there’s no reason not to do this in your own application. I’ve often done similar filters, and have in fact posted very similar code here in answer to various questions.

    It’s hard to imagine a way in which providing dictionary lookup could compromise the system. This functionality isn’t provided by default within Django because of the original desire to have a restricted template language – it’s arguable whether this particular filter should have been provided from the start, but given that it wasn’t, it’s pretty unlikely to be added now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble trying to implement a shared method/property between two classes created by
I am having trouble trying to get some replaces working with my regex. Here
I'm having trouble trying to declare a shared memory array within the kernel. Here's
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having trouble trying to set the value of a property after i cast
Hey having some trouble trying to maintain transparency on a png when i create
I am having real trouble trying to get decent answers to my questions from
I'm writing some code in python and I'm having trouble when trying to retrieve
I'm having a bit of trouble trying to get class variables to work in
Im trying to extract a line from wget's result but having trouble with it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.