从 Django 模板解析会话

Session parse from django templae

我在我的 django 模板中得到了这个。

{{request.session[0]}}

我收到了这个错误:

Could not parse the remainder: '[0]' from 'request.session[0]'

当我在模板中使用 {{request.session}} 时也显示了对象哈希,所以我猜数据传递没问题,当我可以毫无问题地打印会话 [0] 时,为什么它可能无法工作模板?

您可以在模板中使用 . 访问数组元素:

{{request.session.0}}

来自 wiki :

Dot lookups can be summarized like this: when the template system encounters a dot in a variable name, it tries the following lookups, in this order:

Dictionary lookup (e.g., foo["bar"])
Attribute lookup (e.g., foo.bar)
Method call (e.g., foo.bar())
List-index lookup (e.g., foo[2])