0

I am making a api call in my django app. The response is json. Currently I am looping through the data in my template and showing all the data in a table.

All the data shows in the table except for the image_url. The image_url has a url path to a jpg. Do this type of json need to be handled differently than a normal string?

I notice there are a few fields missing data and I cannot figure out why these few fields are missing the data and fields before them and after them are working fine.

This is one of the json objects

[
  {
    "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"
      }
    ]
  },

This is how I am handling the data in the view

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')

This is how I am accessing the data in the template I marked the ones which are missing.

          {% 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 %}

enter image description here

4
  • Sorry, I lost you. You say that the response is json, but the response in your view function is clearly html? Commented Apr 15, 2016 at 6:16
  • The first code block is json. Then in my view I put the json in a list. then I access it in the html template Commented Apr 15, 2016 at 6:17
  • I copied and pasted the first object in the json. This is my code. Are you saying the problem here is the json response I am getting is not valid json? Commented Apr 15, 2016 at 6:24
  • I added a screenshot. From my understanding in the view where I have card I am just creating a key to iterate through the objects. If I am not correct based on the screenshot what would I use to iterate if not card? Commented Apr 15, 2016 at 6:29

1 Answer 1

1

They're not missing, they're not part of card, they're part of an editions object, so you need to iterate over that list.

{% for card in data %}
    {% for edition in card.editions %}
        <td> {{edition.set }}</td>
    {% endfor %}
{% endfor %}
Sign up to request clarification or add additional context in comments.

8 Comments

@wuno - I'd be surprised if it does since its missing the closing tag
It is correct im my app that is a typo in my question. And yes it does indeed show the information in my page. That is what I mean by data is missing before and after . I can see how editions needs to be added to data by why would some show and some not show? Should I just append editions to data and access it in the same way? data.append(editons) card in data card.image_url
How can that work when in my view I am adding the data.append(cards)? Do I not need to add editions some how?
@wuno - I had missed out the card.
I am so so confused. I swear I do not try to have other people do my work for me. This is the third app I have made with django all which used an api call. Will you please help me parse this json so I can understand what I am missing. I really want to understand this to mastery.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.