Question

I just have django python code, for filter template

@register.filter("dict")
def dict(dict, key):
    if(dict and (key in dict)):
        return dict[key]
    else:
        return None

I invoke in html file:

<table>
<tr><th>Mon</th></tr>
        {% for item in COMP|hash:"ITEMS"|hash:"roster" %}
          {{item|dict:"Roster"}}
        {% endfor %}
 </table>

they dict is the dictionary object. but when I run it. It show me error:

argument of type 'Roster' is not iterable

I think the problem is the dict object in template is regarding as list, so it would be not iterable. how to make the python know the object is the dictionary not list?

Was it helpful?

Solution

The problem isn't what you think it is. Python knows exactly what type the object is; you don't need to tell it whether you're working with a list or a dict. I don't know enough about Django to tell what the stuff in the HTML is doing, but the error message seems to indicate that you're passing in something that is neither a list nor a dict. You're actually working with something of type Roster. Figure out why that's happening, and you'll figure out the problem.

OTHER TIPS

Keeping in mind that you should use another name for the dictionary; You can try the following:

isinstance(yourDict, dict)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top