wuno wuno - 6 months ago 20
Python Question

Parsing Api Response Django Module

I am using this magic the gathering api in django. Instead of using requests to call the url I can use the built in functions. But the response is confusing me.

When I call

cards = Card.where(page=50).where(pageSize=500).all()

Then print out

I get data that looks like this in my terminal.

mtgsdk.card.Card object at 0x10696bcc0>, <mtgsdk.card.Card object at 0x10696bcf8>, <mtgsdk.card.Card object at 0x10696bd30>, <mtgsdk.card.Card object at 0x10696bd68>, <mtgsdk.card.Card object at 0x10696bda0>]

I was thinking I maybe need to decode it and it's a dict but I basically throwing darts blindly and have no clue if I am getting any closer.

Someone please shed some light here. What format is this response in and how would I handle it?

def graphs(request):
data = []
cards = Card.where(page=50).where(pageSize=500).all()
mtg_data = str(cards)
return render(request, 'graphs/graphs.html', {'data': data})

Then I am trying to access the card in the template like this, but I get nothing.

<div class="frame" id="basic">
<ul class="clearfix">
{% for cards in data %}
<li><a href="#"><img src="{{cards.image_url }}" /></a></li>
{% endfor %}




You've appended the list of cards to an empty list, so now data is a list consisting of a single item, which is itself a list. Instead, you just want to send the cards list itself to the template.

return render(request, 'graphs/graphs.html', {'data': cards})