Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
accessing dictionary in a template
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
cesco  
View profile  
 More options Jun 16 2007, 9:17 am
From: cesco <fd.calabr...@gmail.com>
Date: Sat, 16 Jun 2007 13:17:07 -0000
Local: Sat, Jun 16 2007 9:17 am
Subject: accessing dictionary in a template
Hi,

I'm using the django-voting application whose method
get_votes_in_bulk(object) returns a "votes" dictionary like:
{object_id: {''score': score, 'num_votes': num_votes}

I'm passing this dictionary as extra_context to a template where I'm
also using the regroup tag on theobjects which I pass as the main
queryset.

So I would need to access the score and the num_votes as follow on a
specific item base (I can't iterate sequentially over the items due to
the regrouping). To be precise I would need to access as follow:

{{ votes.object.id.score }}

Of course this doesn't work because we can only use
votes.object_id.score but not votes.object.id.score. Instead I need to
first lookup the id and then access the dictionary using this id.

If it was python I could have done something like:
votes[object.id]['score']

Is there a way to do so in a template?

Thanks
Francesco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Jun 16 2007, 6:23 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Sun, 17 Jun 2007 08:23:32 +1000
Local: Sat, Jun 16 2007 6:23 pm
Subject: Re: accessing dictionary in a template

No, you can't do indirect references (the vote[object.id] part) like
this in templates.

The usual recommended solution is to rejig the data structures a little
bit when you find yourself needing to do this. Instead of using the
value of one data structure to index into a second structure, put them
all into one data structure so that all the information is available at
once. How to do this depends on the particular application -- there
isn't always a general recipe.

Regards,
Malcolm


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »