Re: Displaying dictionaries

2010-05-18 Thread derek
On May 18, 11:35 am, Tom Evans  wrote:
> On Tue, May 18, 2010 at 12:46 AM, Barry  wrote:
> > Hi--
>
> >  I want to dynamically display the contents of results (an array of
> > dictionaries with the same keys) as a table where the column headers
> > are a select group of keys in the variable result_col_titles and the
> > order of the columns is the same as the order of the keys in
> > result_col_titles. I imagine the template code would be something like
> > what is below but I don't know how to reference a dictionary item
> > value when the key value is in a variable and not hard-coded in the
> > template.
>
> >  This seems pretty basic but I didn't see it in the Definitive Guide
> > to Django.
>
> > Thanks
>
> > Barry
> > ===
> >      
> >        
> >          {% for title in result_col_titles %}
> >            
> >               {{ title }}
> >            
> >          {% endfor %}
> >        
> >        {% for dictionary in result %}
> >          
> >            {% for title in result_col_titles %}
> >              
> >                 {{ dictionary.title }}
> >                 {{ dictionary.{{title}} }}
> >              
> >            {% endfor %}
> >          
> >        {% endfor %}
> >      
>
> It's considered 'poor form' by django devs - too much work in the
> template. If you want the data in that order, arrange it so in the
> view.
>
> If you don't agree, add this 5 line template tag:
>
>   from django import template
>   register = template.Library()
>
>   @register.filter
>   def dict_get(hash, key):
>     return hash[key]
>
> It should go in /templatetags/dict_get.py
>
> You can then do:
>
> {% load dict_get %}
>
> {% for dict in list_of_dicts %}
> 
> {% for title in titles %}
>   {{ dict|dict_get:title }}
> {% endfor %}
> 
> {% endfor %}
>
> Cheers
>
> Tom

Having _just_ struggled for a week with this and finally discovering a
link here:
http://www.bhphp.com/blog4.php/2009/08/17/django-templates-and-dictionaries
that saved my sanity, I curious as to why this is considered "too much
work".  All the data in Django seems to be passed around via
dictionaries (or, rather, if you do any kind of interim processing on
it in the view logic, it ends up in lists of dictionaries) , so it
would seem logical to allow sorting by dictionary keys.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Displaying dictionaries

2010-05-18 Thread Tom Evans
On Tue, May 18, 2010 at 12:46 AM, Barry  wrote:
> Hi--
>
>  I want to dynamically display the contents of results (an array of
> dictionaries with the same keys) as a table where the column headers
> are a select group of keys in the variable result_col_titles and the
> order of the columns is the same as the order of the keys in
> result_col_titles. I imagine the template code would be something like
> what is below but I don't know how to reference a dictionary item
> value when the key value is in a variable and not hard-coded in the
> template.
>
>  This seems pretty basic but I didn't see it in the Definitive Guide
> to Django.
>
> Thanks
>
> Barry
> ===
>      
>        
>          {% for title in result_col_titles %}
>            
>               {{ title }}
>            
>          {% endfor %}
>        
>        {% for dictionary in result %}
>          
>            {% for title in result_col_titles %}
>              
>                 {{ dictionary.title }}
>                 {{ dictionary.{{title}} }}
>              
>            {% endfor %}
>          
>        {% endfor %}
>      
>

It's considered 'poor form' by django devs - too much work in the
template. If you want the data in that order, arrange it so in the
view.

If you don't agree, add this 5 line template tag:

  from django import template
  register = template.Library()

  @register.filter
  def dict_get(hash, key):
return hash[key]

It should go in /templatetags/dict_get.py

You can then do:

{% load dict_get %}

{% for dict in list_of_dicts %}

{% for title in titles %}
  {{ dict|dict_get:title }}
{% endfor %}

{% endfor %}


Cheers

Tom
Hope that helps.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.