如何在django模板中获取字典中的第一个值?

时间:2016-05-27 07:42:51

标签: django django-templates

{
   "unknown1": 
   [
        {"text": "random text again",
        "time": "Thu May 15 19:21:59 +0000 2016"}, 
        "text": "akmfkdlm safsa fasffalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"}
   ],
   "unknown2":
   [ 
        "text": "fsda lmfalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"},
   ]
}

这个字典正被发送到我的django模板。 我怎样才能获得import json import requests with open('locations_stripped.json') as data_file: data = json.load(data_file) headers = {'Content-Type' : 'application/json'} for list_values in data.values(): for dict_element in list_values: text = dict_element['text'] print text body = { "text": text , "mime_type": "text/html", "extract_type": "HP"} r = requests.post('localhost:3003/api/extract/run', json=body, headers=headers) print (r.content)

注意:我不知道a = {'key1':'value1', 'key2':'value2', 'key3':'value3'...} ,因为它是动态生成的。

1 个答案:

答案 0 :(得分:1)

使用first方法使用dict.values过滤器:

{{ a.values|first }}

但请记住:dict是无序的。当然,您可以使用OrderedDict

OrderedDict使用make_list过滤器:

{{ a.values|make_list|first }}