librelist archives

« back to archive

Problems rendering dictionary

Problems rendering dictionary

From:
Adam Oakman
Date:
2010-12-06 @ 17:06
Hi,
     I am having an issue rendering a dictionary in my template. I can  
dump the dictionary out in the template but when I try and loop  
through its contents I either get no output for the variable or a  
message <built-in method title of str object at 0x24939e0>. The  
baffling thing to me is I have other dictionaries that are working  
fine. Does anyone have any ideas on troubleshooting?

The sample dictionary is:
{
     '378': {'item_id': '378', 'budget_amt': 2560.0, 'parent_budget':  
False, 'categories': {}, 'title': u'Dept1'},
     '375': {'item_id': '375', 'budget_amt': 5000.0, 'parent_budget':  
False, 'categories': {}, 'title': u'Dept2'},
     '377': {'item_id': '377', 'budget_amt': 10000.0, 'parent_budget':  
False, 'categories': {}, 'title': u'Dept3'},
     '376': {'item_id': '376', 'budget_amt': 2580.0, 'parent_budget':  
False, 'categories': {}, 'title': u'Dept4}
}

Sample template code with dictionary passed in as budget_items:
	{% for item in budget.budget_items %}
             <div id="dept_{{ item.item_id }}" class="dept">
                 <a href="" class="edit_item"  
id="ei_{{ item.item_id }}">Edit</a>
                 <h3 id="item_{{ item.item_id }}">{{ item.title }}  
{{ item.budget_amt }}</h3>
                 {% for cat in item.categories %}
                     <div id="item_{{ cat.item_id }}">
             		{{ cat.title  }} ${{ cat.budget_amt }}
             		<a href="" class="edit_item"  
id="edit_item_{{ cat.item_id }}">Edit</a>
		    </div>
                 {% else %}
                     <p>Add a category</p>
                 {% endfor %}
             </div>
         {% else %}
             <p id="add_prompt"><em>Now lets <a href=""  
class="add_dept">create a department</a> for this budget</em></p>
         {% endfor %}

Thanks

Adam

Re: [flask] Problems rendering dictionary

From:
Tim Golden
Date:
2010-12-06 @ 17:09
On 06/12/2010 17:06, Adam Oakman wrote:
> Hi,
>       I am having an issue rendering a dictionary in my template. I can
> dump the dictionary out in the template but when I try and loop
> through its contents I either get no output for the variable or a
> message<built-in method title of str object at 0x24939e0>. The
> baffling thing to me is I have other dictionaries that are working
> fine. Does anyone have any ideas on troubleshooting?

If you iterate over a dictionary, you receive the keys,
not the values (nor the items). So you're getting back:

'378'
'375'

etc.

By default flask/jinja2 will silently ignore any missing attributes,
such as "item_id" but since since ".title" really is an attribute
of a string -- the method which converts it to titlecase -- it
correctly returns that object: the method.

TJG

Re: [flask] Problems rendering dictionary

From:
Adam Oakman
Date:
2010-12-06 @ 21:58
Of course, thanks heaps Tim! Added .values() and all is OK.

On Dec 6, 2010, at 11:09 AM, Tim Golden wrote:

> On 06/12/2010 17:06, Adam Oakman wrote:
>> Hi,
>>      I am having an issue rendering a dictionary in my template. I  
>> can
>> dump the dictionary out in the template but when I try and loop
>> through its contents I either get no output for the variable or a
>> message<built-in method title of str object at 0x24939e0>. The
>> baffling thing to me is I have other dictionaries that are working
>> fine. Does anyone have any ideas on troubleshooting?
>
> If you iterate over a dictionary, you receive the keys,
> not the values (nor the items). So you're getting back:
>
> '378'
> '375'
>
> etc.
>
> By default flask/jinja2 will silently ignore any missing attributes,
> such as "item_id" but since since ".title" really is an attribute
> of a string -- the method which converts it to titlecase -- it
> correctly returns that object: the method.
>
> TJG