开发者

JSON response not a true string when passed to HTML template?

I'm confused by the following behavior:

Using Freebase-Python, I send a request to the Freebase API that is a query for a JSON response. I get the response, for example:

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

Within the same RequestHandler class that I used issue the request, I can do things like,

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

However, when I pass the result object to an HTML template, the weird behavior starts.

I can do,

{{ result.id }}

Which will render "/en/settlers_of_catan" in the browser. But if I try,

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

I get an error:

  raise开发者_如何学Go TemplateSyntaxError, "Could not parse the remainder: %s" % token[upto:]
TemplateSyntaxError: Could not parse the remainder: ["/common/topic/weblink"][0].url

I can also just display the result:

{{ result }}

Which results in the browser:

{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'}

My question is, why can't I access the result in the HTML template the same way I can from the RequestHandler?


In the django template language dictionary-, list index and attribute lookups are made using a dot ('.').

For that reason it should be something like {{ result.mylink.0.url }}, but this will most like not work using slashes in the key!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜