Access values from a dict in a function called in a Jinja expression

╄→尐↘猪︶ㄣ 提交于 2020-01-15 20:17:18

问题


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 %}

回答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.




回答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'].



来源:https://stackoverflow.com/questions/36090767/access-values-from-a-dict-in-a-function-called-in-a-jinja-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!