How to use Django with python-requests to fetch data from google api?

514 views
Skip to first unread message

Krystian

unread,
May 9, 2019, 8:35:12 PM5/9/19
to Django users
Hi there,

I was trying today just for myself learning to get data from google books api https://www.googleapis.com/books/v1/volumes/ without needed auth key but the point is that I'm getting no results. I've tried with single book (like below), tried to get books that title contains "something" but django is giving me key errors when I'm trying to display it on view. Could you please tell me how to make this work with Django? I know it should be simple but still gettings those errors

Exception Type:KeyError
Exception Value:
'title'

import requests

def api(request):
    book = response.json()
    return render(request, 'books/api.html', {
        'title': book['title'],
        'authors': book['authors']
    })

and here is html

{% block content %}
  <h2>Google books API</h2>
  <form method="get">
    <input type="text" name="book">
    <button type="submit">search on google books api</button>
  </form>
  {% if book %}
      <p>
        <strong>{{ book.title }} {{ book.authors }}
      </p>
  {% endif %}
{% endblock %}

Joe Reitman

unread,
May 10, 2019, 1:04:17 AM5/10/19
to Django users
Krystian,

To extract the Title and Author from your API call you need:

return render(request, 'books/api.html', {
       
'title': book['volumeInfo']['title'],
       
'authors': book['volumeInfo']['authors'][0]
    })


'authors' is stored in an array(list) so if you want to pass all the authors as text you'll have to do some sort of list comprehension or just get one with an index.

Regards,
Joe

Krystian

unread,
May 10, 2019, 4:01:16 AM5/10/19
to Django users
Hi Joe,

Thanks you for clarying :) anyway now is KeyError and i dont know why the request is wrong, should I add the "items" before? Because https://jsoneditoronline.org/ shows that this data from google books api looks like this and maybe request is wrong?

objectitems0volumeInfoauthors

KeyError at /api

'volumeInfo'

Krystian

unread,
May 10, 2019, 4:24:53 AM5/10/19
to Django users
Hey,

I've updated some code and now I think it should work but the problem is I guess with list book = {}

def api(request):
    book = {}
    if 'book' in request.GET:
        book = request.GET['book']
        reponse = requests.get(url)
        book = response.json()
    return render(request, 'books/api.html', {'book': book})

But I'm getting error

name 'response' is not defined

but the get request is correct with status 200, but I'm not sure how to fetch only fields title and authors, I guess the problem is that because on the search results are few titles and authors, what do you think Joe?

  1.         book = response.json()
    ...
VariableValue
book
'frodo'
reponse
<Response [200]>
request
<WSGIRequest: GET '/api?book=frodo'>
url
'https://www.googleapis.com/books/v1/volumes?q=frodo'

Krystian

unread,
May 10, 2019, 4:46:46 AM5/10/19
to Django users
Hey Joe,

There was a spelling mistake :D i've been sitting so long on this and didn't saw it 

Joe Reitman

unread,
May 10, 2019, 2:48:47 PM5/10/19
to Django users
Krystian,

I got this work by changing the template tag name variables and removing the if statement. Since 'authors' is a list, you could send the entire list to the template and do a for-loop to render all the authors.

def api(request):
 query
= request.GET
 response
= requests.get('https://www.googleapis.com/books/v1/volumes/' + query['book'])

 book
= response.json()
 
return render(request, 'books/api.html', {

 
'title': book['volumeInfo']['title'],
 
'authors': book['volumeInfo']['authors'][0]
 
})


<body>

{% block content %}
<h2>Google books API</h2>
<form method="get">
 
<input type="text" name="book">
 
<button type="submit">search on google books api</button>
</form>
<p>
 
<strong>{{ title }} - {{ authors }} </strong>
</p>
{% endblock %}
</body>

Output

Screen Shot 2019-05-10 at 9.47.13 AM.png

Reply all
Reply to author
Forward
0 new messages