Reputation: 11
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 attribute 'eId'
. Why did the second access fail when the first succeeded?
@app.route('/')
def show_entries():
if session.get('logged_in'):
cur = g.db.execute('select title, text, id from entries1 WHERE userid = ? OR public = 1 order by id desc', [userInfo['userid']])
else:
cur = g.db.execute('select title, text, id from entries1 WHERE public = 1 order by id desc')
entries = [dict(title=row[0], text=row[1], eId=row[2]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries)
{% for entry in entries %}
This works: {{ entry.eId }}
This errors: {{ url_for('delete_entry', btnId=entry.eId) }}
{% endfor %}
Upvotes: 1
Views: 1858
Reputation: 11711
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']
.
Upvotes: 2
Reputation: 973
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.
Upvotes: 2