In my Django template, I call this frequently:
{% for name, address in directory.addressbook.items %}
{% for street in address.list %}
{{street.number}}
How do I create this as a filter? I've tried this, but it doesn't work.
In the template, I call:
{{directory.addressbook.items|all_numbers}}
And in my filter definition I have:
def all_numbers(data):
number_list=[]
if isinstance(data, dict):
for name, address in data:
for street in data.list():
number_list.append(street)
return number_list
But all I get returned is "[ ]". What am I doing wrong?
for name, address in data:) is not getting used at all (you don't usenameoraddress).dicthas alistattribute. This should raise a syntax error. since you aren't seeing that it makes me thing that part of the code isn't being executed. Isdataempty? If so, then returning[]is correct.for street in address.list(). But I don't get an exception there either - whats the right way to do this? (sorry, i'm new to python)streetmight be an object with alistmethod, so that might be OK