I have a context variable that contains a dictionary, which was created via defaultdict(list)
.
The dictionary's anatomy is like so:
eachlink = {'seen':[object1, object2, None], 'unseen':[None, object2, object3], 'time':[timeobject1, None, timeobject3]}
'seen', 'unseen' and 'time' are lists containing objects (and an object can be None
too).
This dictionary is passed to the context object in views.py:
context["eachlink"] = eachlink
In my template, I'm trying to use the data contained within the context variable eachlink
, but can't get the dot notation to work. E.g. the following does NOT work (the for loop
never runs, nor the if statement
ever fires):
{% for object in eachlink %}
{% if object.seen %}
do something
{% endif %}
{% if object.unseen %}
do something else
{% endif %}
{% if object.time %}
do something else
{% endif %}
{% endfor %}
I also tried the following, which didn't work:
{% for seen, unseen, time in eachlink %}
{% if seen %}
do something
{% endif %}
{% endfor %}
In the above, debugging led me to the finding that 'seen' contains the letters 'seen', unseen contains the letters 'unseen' and time contains the letters 'time'. Clearly, the lists within the dictionary aren't being referenced. I've seen suggestions on SO to use eachlink.items
instead of eachlink
, I've also seen suggestions to write custom templatetags
. Both approaches haven't fit my case after multiple hours of trying.
I've printed my dictionary via views.py; it's being constructed fine. Accessing it in the template remains a challenge. What should I do? Should I pursue an alternative way to organize this data in my context variable? Suggestions please.
I'm using Django 1.5
and Python 2.7
.