Whats the point of having dicts in templates if one cannot access them by key?

44 views
Skip to first unread message

Juan Pablo Tamayo

unread,
Nov 13, 2012, 11:40:20 PM11/13/12
to django...@googlegroups.com
Let me explain, I have a nested dictionary like:

agenda = {'3': {'2012-11-11': <MyObject>, '2012-11-14': <MyObject>, ...},
  '7': {'2012-11-9': <MyObject>, },
  '2': {'2012-10-28': <MyObject>},
  }

And I want to access it by specifying the two keys. I've red that i must access the dict like (where attribute is an attribute of <MyObject>:

{% for k, a_dict in agenda.items %}
  {{ k }}
  {% for k2, v in a_dict.items %}
    {{ k }}-{{ k2 }}-{{ v.attribute }}
  {% endfor %}
{% endfor %}

But, whats the point of using dicts if I have to access the values in whatever order the <Dict>.items() gives me the keys and values?

What if I need to access these values in a specified order? (for the outer dict AND the inner ones)

Is there a way to access a two-dimensional container IN AN PRE-SPECIFIED ORDER?

Thaks for any advise.

pablete

Lachlan Musicman

unread,
Nov 14, 2012, 1:31:14 AM11/14/12
to django...@googlegroups.com
I used the SortedDict datastructure :)

https://docs.djangoproject.com/en/dev/ref/utils
or
/path/django-docs/ref/utils.html#django.utils.datastructures.SortedDict
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/y5GJm9E1D4YJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, “We
look at the present through a rear-view mirror. We march backwards
into the future.”

http://www.warrenellis.com/?p=14314
Message has been deleted

Bill Freeman

unread,
Nov 14, 2012, 11:37:55 AM11/14/12
to django...@googlegroups.com

I believe that if the keys were valid python identifiers you could access them using dot notation.  E.g.; if a_dict is dict(a=dict(aa='foo')), then in the template you could say {{ a_dict.a.aa }}.  This may work with the simple numbers, but with the keys containing minuses there may be problems.  You could try quoting them.

If none of that stuff is going to work for you, you aught to be able to write a simple template filter that filters the dict, and takes the key as a quoted fight hand argument.  You then cascade this for nested dictionaries.

But if it were me, I'd probably see if I couldn't do it in the view, producing lists or tuples with the stuff in the order you need them.

Bill

Juan Pablo Tamayo

unread,
Nov 14, 2012, 12:32:59 PM11/14/12
to django...@googlegroups.com

Thank you all. I have fix my issue icons sorteddict from the Python collections.

Juan Pablo Tamayo H
Sent from my Android phone

--
You received this message because you are subscribed to the Google Groups "Django users" group.
Reply all
Reply to author
Forward
0 new messages