0

I have a dictionary with a number of characteristics:

sort_options = SortedDict([ ("importance" , ("Importance" , "warning-sign" , "importance")), ("effort" , ("Effort" , "wrench" , "effort")), ("time_estimate" , ("Time Estimate" , "time" , "time_estimate")), ]) 

I also have a list of actions as a query result. Each action has these attributes; In my template, I can call {{ action.effort }} or {{ action.time_estimate }} and get a result.

I'm iterating through my sort_options to populate twitter bootstrap icons:

{% for key, icon in sort_options.items %} <i class="icon-{{ icon.1 }}"></i> {% endfor %} 

But I also want to display the action value for each of these attributed. Essentially, something like:

{% for key, icon in sort_options.items %} <i class="icon-{{ icon.1 }}"></i> {{ action.key }} {% endfor %} 

Where key would resolve to "importance" or "effort". I know this doesn't work. So I was trying to leverage the solution presented in this question.

The solution proposed a template filter:

def hash(h,key): if key in h: return h[key] else: return None register.filter(hash) {{ user|hash:item }} 

Where the question used a dictionary that looked like so:

{'item1': 3, 'name': 'username', 'item2': 4} 

I tried the following:

{% for key, icon in sort_options.items %} <i class="icon-{{ icon.1 }}"></i> {{ action|hash:key }} {% endfor %} 

But got an error:

Caught TypeError while rendering: argument of type 'Action' is not iterable 

I believe this is because the template filter is getting just one attribute of the object (likely the name) as opposed to the whole dictionary:

[<Action: Action_one>, <Action: Task_two>...] 

Is there a way to force the template to pass the full object to the template tag?

6
  • 1
    Please make sure that you mean a template tag but not a template filter. Commented Aug 13, 2012 at 19:19
  • 1
    Ignore that other question. Exactly what are you trying to achieve? Commented Aug 13, 2012 at 19:23
  • Apologies for being too hasty with the question. It is greatly clarified above Commented Aug 13, 2012 at 19:52
  • 1
    For future reference, mentioning that you're iterating over actions and then iterating over sort_options for each action would have been handy. Finally, something that looks like [.., ..] is a list, not a dictionary. Your "whole dictionary" is better described as a "list of Action objects`. Commented Aug 13, 2012 at 20:11
  • Can't u just calculate those 'action.key's in your views, and then try sending them as dictionaries to the templates so u won't have to find action.key in your templates ? Commented Aug 13, 2012 at 20:15

1 Answer 1

0

I think I finally get it. You want the equivalent of

getattr(action, key) 

in your template. This answer describes the getattribute templatetag you'd need to do so.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.