我正在我的django应用程序中进行api调用。回应是json。目前,我正在遍历模板中的数据并显示表中的所有数据。
除image_url
外,所有数据均显示在表格中。 image_url
有一个jpg的url路径。这种类型的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 %}
答案 0 :(得分:1)
他们不会错过,他们不属于card
,他们是editions
对象的一部分,因此您需要遍历该列表。< / p>
{% for card in data %}
{% for edition in card.editions %}
<td> {{edition.set }}</td>
{% endfor %}
{% endfor %}