0

I am new in django rest api framework and using get i am fetching the a json array whose api is this https://api.coursera.org/api/courses.v1?q=search&query=machine+learning and i am not able to parse it.Actually i want to store all the names and send them to .html file .I have used this code but didnot worked for me.

    req = requests.get('https://api.coursera.org/api/courses.v1?q=search&
                                query=machine+learning')
    jsonList = []
    jsonList.append(req.json())
    print(jsonList[0])
    userData = {}
    for value in jsonList[0]:
        parsedData.append(value["name"])
        print(value["name"])
    return render(request, 'app/profile.html', {'data': parsedData})
2
  • How did it "not work for you"? Please don't just say that it didn't work. Include the expected behavior and what behavior you saw. Commented May 8, 2016 at 13:17
  • i was getting this error Exception Value: string indices must be integers Commented May 8, 2016 at 13:23

2 Answers 2

6

This actually has nothing to do with Django.

The way to get to the name attribute inside elements (there are actually many elements, each one has a 'name`):

import requests
import json

req = requests.get('https://api.coursera.org/api/courses.v1?q=search&query = machine + learning')

json_data = json.loads(req.text)

for element in json_data['elements']:
    print(element['name'])

>> Speak English Professionally: In Person, Online & On the Phone
   Machine Learning
   Learning How to Learn: Powerful mental tools to help you master tough subjects
   .
   .

Update:

To display the names in a view:

Considering that you have a very basic template:

{% for name in names %}
    <p>{{ name }}</p>
{% endfor %}

Inside your view, considering you already have the above code, and you stored all the names in a list called names:

return render(request, 'app/profile.html', context={'names': names}, status=200)  
# it's always a good habit to return an HTTP status code 
Sign up to request clarification or add additional context in comments.

1 Comment

DeepSpace I donot want to print in console.i want to store all name and send to .html file so that i can run a loop and display those names, so how to do this,please help me.
0

In case you have a Json Array without A Name/Key as shown below you can do, it is very similar to accepted answer below with slight difference of not having key/name in the Json Array

[
    {
        "id": 7,
        "user": 2129
    },
    {
        "id": 8,
        "user": 2129
    }
]

In View

response = requests.post('http://127.0.0.1:8000/api/some-api/', headers=headers)

users = json.loads(response.text)
for user in users:
    print(user)

return render(request, "web_client/mine.html", {"data": users, "page": "mine"})

In Template

{% for row in data %}
  {{ row.some_key_to_print }}
{% endfor %}

Comments

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.