0

我对以下行为感到困惑:

使用 Freebase-Python,我向 Freebase API 发送一个请求,该请求是对 JSON 响应的查询。我得到响应,例如:

  "status": "200 OK", 
  "code": "\/api\/status\/ok", 
  "result": {
    "\/common\/topic\/weblink": [
      {
        "url": "http:\/\/www.boardgamegeek.com\/boardgame\/13\/Settlers of Catan", 
        "description": "BoardGameGeek"
      }
    ], 
    "id": "\/en\/settlers_of_catan"
  }

在我用来发出请求的同一个 RequestHandler 类中,我可以执行以下操作,

print result.id 
>>> /en/settlers_of_catan
print result["/common/topic/weblink"][0].url
>>> http://www.boardgamegeek.com/boardgame/13/Settlers of Catan

但是,当我将结果对象传递给 HTML 模板时,就会出现奇怪的行为。

我可以,

{{ result.id }}

这将在浏览器中呈现“/en/settlers_of_catan”。但如果我尝试,

{{ 结果["/common/topic/weblink"][0].url }}

我收到一个错误:

  raise TemplateSyntaxError, "Could not parse the remainder: %s" % token[upto:]
TemplateSyntaxError: Could not parse the remainder: ["/common/topic/weblink"][0].url

我也可以只显示结果:

{{ result }}

这导致浏览器:

{u'/common/topic/weblink': [{u'url': u'http://www.boardgamegeek.com/boardgame/13/Settlers of Catan', u'description': u'BoardGameGeek'}], u'id': u'/en/settlers_of_catan'}

我的问题是,为什么我不能像从 RequestHandler 一样访问 HTML 模板中的结果?

4

1 回答 1

2

django 模板语言字典中,列表索引和属性查找是使用点 ('.') 进行的。

出于这个原因,它应该是类似的{{ result.mylink.0.url }},但这最喜欢在键中使用斜杠不起作用!

于 2010-12-20T03:00:46.997 回答