当我将object['userinfo']
发布到Jinja2 HTML时,它会呈现此信息。
[{
'enemies':[
],
'url':'URL',
'friends':[
{
'area':2,
'same_interest':6
}
]
}]
所以,如果我得到object['userinfo'][0]
,它将返回对象:
{
'enemies':[
],
'url':'URL',
'friends':[
{
'area':2,
'same_interest':6
}
]
}
但是一旦我试图从对象(object['userinfo'][0]['url']
)获取URL值,
我收到一个错误:
jinja2.exceptions.UndefinedError: list object has no element 0
即使我可以在没有任何其他键的情况下访问“0”元素。
编辑: 完整错误追溯:
[2018-05-11 17:02:30,617] ERROR in app: Exception on /start [POST]
Traceback (most recent call last):
File "/flask/venv/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/flask/venv/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/flask/venv/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/flask/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/flask/venv/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/flask/venv/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/flask/app.py", line 117, in startreporting
return render_template("report.html", items=loaded_r)
File "/flask/venv/lib/python3.5/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/flask/venv/lib/python3.5/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/flask/venv/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/flask/venv/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/flask/venv/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/flask/templates/report.html", line 29, in top-level template code
<td>{{ x['userinfo'][0]['url'] }}</td>
File "/flask/venv/lib/python3.5/site-packages/jinja2/environment.py", line 411, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: list object has no element 0
127.0.0.1 - - [11/May/2018 17:02:30] "POST /start HTTP/1.1" 500 -
如何访问对象的第一个数组中的键?