0

次の動作に混乱しています。

Freebase-Python を使用して、JSON 応答のクエリである Freebase API に要求を送信します。たとえば、次のような応答が返されます。

  "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 に答える