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




On Thursday, May 9, 2019 at 3:35:12 PM UTC-5, Krystian wrote:
>
> 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):
>     response = requests.get('
> https://www.googleapis.com/books/v1/volumes/YyXoAAAACAAJ')
>     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 %}
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/716b7d01-956d-42ae-8719-72c4365c12ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to