Skip to content Skip to sidebar Skip to footer

Access Values From A Dict In A Function Called In A Jinja Expression

I'm passing a dict from a Flask view to a Jinja template. I can render the values in the dict, but if I try to pass them to url_for I get UndefinedError: 'dict object' has no attr

Solution 1:

Instead of "{{ url_for('delete_entry', btnId= entry.eId) }}" you should have "{{ url_for('delete_entry', btnId= entry['eId']) }}" because elements in a dictionary should be accessed by their get method. The only reason that {{ entry.title }} works is because of jinja2.

Essenially {{ entry.title }} gets evaluated by jinja whereas "{{ url_for('delete_entry', btnId= entry.eId) }}" gets evaluated by python and breaks.

Solution 2:

Your entry is a dictionary. While Jinja's expression syntax allows you to use attribute syntax (dict.attr) for dictionaries, as soon as you're passing an argument to a function with Python syntax you need to use Python's normal dictionary access syntax, dict['attr'].

Post a Comment for "Access Values From A Dict In A Function Called In A Jinja Expression"