Reputation: 993
i have a view that returns a dict. I searched around SO and found : Django template how to look up a dictionary value with a variable
this is the return statement of my view:
return render(request, 'App/index.html', context={'talks': word_count})
I´m pretty new in django so i think this is why i do not notice the error here. The answer to the mentioned question is this one, create a filter and load it in the template, ok, this is the filter:
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
And here is where i am using it in the wrong way:
<ul>
{% for talk in talks %}
<li>{{ talks|get_item:talk.count }}</li>
{% endfor %}
</ul>
This is the dictionary i have:
{' - Nacho:': 1211, ' - Pato:': 1950, ' - Nicolas:': 1871, '}
the way i created it is this one:
lista = {}
for line in lines:
if user in line:
count += 1
lista[user] = count
simple as that. The error i´m having is this one:
All the elements appear like this:
. None
. None
. None
not knowing exactly how to use the filter is making it hard to fix this Maybe someone with more experience in django can point me in the right direction
Any help will be really appreciated
Upvotes: 1
Views: 918
Reputation: 599638
Your dictionary items do not have a count
attribute.
But I don't know why you think you need this filter at all. You have a simple dictionary, not a complex nested data structure; if you simply want the key (name) and value (count) for each element, just iterate through with items
:
{% for name, count in talks.items() %}
<li>{{ name }} - {{ count }}</li>
{% endfor %}
Upvotes: 3