我在我的django應用程序中進行api調用。答案是json。目前我正在循環瀏覽模板中的數據並顯示錶格中的所有數據。在Django中處理Json響應
除image_url
以外的所有數據均顯示在表格中。 image_url
有一個JPG路徑。這種類型的json需要與正常的string
不同嗎?
我注意到有幾個字段缺少數據,我無法弄清楚爲什麼這幾個字段缺少數據和字段之前,他們都工作正常。
這是JSON對象之一
[
{
"name": "_____",
"id": "_____",
"url": "https://api.deckbrew.com/mtg/cards/_____",
"store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW",
"types": [
"creature"
],
"subtypes": [
"shapeshifter"
],
"colors": [
"blue"
],
"cmc": 2,
"cost": "{1}{U}",
"text": "{1}: This card's name becomes the name of your choice. Play this ability anywhere, anytime.",
"power": "1",
"toughness": "1",
"formats": {},
"editions": [
{
"set": "Unhinged",
"set_id": "UNH",
"rarity": "uncommon",
"artist": "Ron Spears",
"multiverse_id": 74252,
"flavor": "{1}: This card's flavor text becomes the flavor text of your choice. (This ability doesn't work because it's flavor text, not rules text (but neither does this reminder text, so you figure it out).)",
"number": "23",
"layout": "normal",
"price": {
"low": 0,
"median": 0,
"high": 0
},
"url": "https://api.deckbrew.com/mtg/cards?multiverseid=74252",
"image_url": "https://image.deckbrew.com/mtg/multiverseid/74252.jpg",
"set_url": "https://api.deckbrew.com/mtg/sets/UNH",
"store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW"
}
]
},
這是我如何在視圖中
def graphs(request):
if request.user.is_authenticated():
data = []
r = requests.get('https://api.deckbrew.com/mtg/cards')
jsonList = r.json()
for cards in jsonList:
data.append(cards)
return render(request, 'graphs/graphs.html', {'data': data})
else:
return redirect('index')
這是我如何在模板 標誌着我訪問數據處理數據那些失蹤的人。
{% for card in data %}
<tr>
<td>{{ card.name }}</td>
<td>{{ card.id }}</td>
<td>{{ card.url }}</td>
<td>{{ card.store_url }}</td>
<td>{{ card.types }}</td>
<td>{{ card.subtypes }}</td>
<td>{{ card.colors }}</td>
<td>{{ card.cmc }}</td>
<td>{{ card.cost }}</td>
<td>{{ card.text }}</td>
<td>{{ card.power }}</td>
<td>{{ card.toughness }}</td>
<td>{{ card.formats }}</td>
<td>{{ card.editions }}</td>
<td>{{ card.set }}</td> # MISSING
<td>{{ card.set_id }}</td> # MISSING
<td>{{ card.rarity }}</td> # MISSING
<td>{{ card.artist }}</td> # MISSING
<td>{{ card.multiverse_id
<td>{{ card.flavor }}</td> # MISSING
<td>{{ card.number }}</td> # MISSING
<td>{{ card.layout }}</td> # MISSING
<td>{{ card.price }}</td> # MISSING
<td>{{ card.low }}</td> # MISSING
<td>{{ card.median }}</td> # MISSING
<td>{{ card.high }}</td> # MISSING
<td>{{ card.url }}</td>
<td>{{ card.image_url }}</td> # MISSING
<td>{{ card.set_url }}</td> # MISSING
<td>{{ card.store_url }}</td>
</tr>
{% endfor %}
對不起,我失去了你。你說響應是json,但你的視圖函數中的響應顯然是html? – Wtower
第一個代碼塊是json。然後在我看來,我把json放在了一個列表中。然後我在html模板中訪問它 – wuno
我複製並粘貼了json中的第一個對象。這是我的代碼。你說這裏的問題是我得到的JSON響應是不是有效的JSON? – wuno