0

I am trying to use the OpenDOTA API in my pet project. At the moment, I am having problem displaying the content of the API into my CBV.

My views.py:

from django.views.generic import TemplateView

import requests
import json

# Create your views here.


class HeroList(TemplateView):
   template_name = 'dota/heroes.html'
   url = 'https://api.opendota.com/api/heroes'
   r = requests.get(url)
   r.text
   result = r.json()

I am lost on how to call the json in my HTML. I've tried running the same code in python IDLE, and when I type the "result" and hit enter, it gives my the dict. Any idea on how should I display the dict into my template?

tango ward
  • 307
  • 1
  • 4
  • 18
  • Are you sure this is the correct syntax and that you are implementing Django's class-based views properly? Here are the docs: https://docs.djangoproject.com/en/2.0/topics/class-based-views/intro/ – anmaxvl Dec 27 '17 at 00:01
  • Hi, thanks. I am new to Django. I was just guessing how the integration of API works in CBV. I will fix my code. – tango ward Dec 27 '17 at 07:56

2 Answers2

0

If you mean accessing the result in html, then below is a sample.

choices = {'key1':'val1', 'key2':'val2'}

Here's the template:

<ul>
{% for key, value in choices.items %} 
  <li>{{key}} - {{value}}</li>
 {% endfor %}
</ul>

from this answer how to access dictionary element in django template?

johnII
  • 1,423
  • 1
  • 14
  • 20
0

What you need to do is first dump your json to a dictionary format.

import json
from django.shortcuts import render

rdict = json.loads(r.json())

return render(request, template_name=<template name>, context=rdict)

All this reside insides your function inside your views.py

Now after this using Django template language - https://docs.djangoproject.com/en/1.11/topics/templates/

You can render data in keys in your dictionary to your template.

rohit keshav
  • 305
  • 2
  • 16
  • Thanks. I want to use CBV instead of FBV in my views.py. – tango ward Dec 27 '17 at 07:56
  • Hi, it seems that this line will turn the dict into a list. rdict = json.loads(r.json()) – tango ward Dec 27 '17 at 21:08
  • Right that's also fine. Using CBV you'd rather use django generics. The Get view in that. Override the get function in it. Do you want to mod my answer to the same.? – rohit keshav Dec 28 '17 at 22:00
  • About json.loads() function returning a list, that returns what ever structure the API contains. The .loads function doesn't change the structure. It just deserializes from JSON to python data structure. In this case a list – rohit keshav Dec 28 '17 at 22:01
  • Yes rohittk239. Thanks for your inputs – tango ward Dec 29 '17 at 19:45
  • Sorry for follow up question Rohit, I was able to display the strings in my html page by overriding the get function in my CBV. Problem is it is a bunch of strings. I know it might not be related to the initial question but can you point me to the right direction on how to manipulate json in html? I can filter the keys of dict using python IDLE (e.g. data['match_id']) but this format doesn't work in Django. Any idea? – tango ward Dec 29 '17 at 20:10
  • No problem, data.match_id would give you access to 'match_id' key inside data. No when you say you want to filter the data, you can either use for loop with if in the Django template or pre filter the data and pass it as a new context to your HTML. – rohit keshav Jan 02 '18 at 00:23
  • Thanks Rohit, you've been very helpful. – tango ward Jan 02 '18 at 08:43