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?
sort_optionsfor eachactionwould have been handy. Finally, something that looks like[.., ..]is a list, not a dictionary. Your "whole dictionary" is better described as a "list ofActionobjects`.