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?
object►items►0►volumeInfo►authors►
KeyError at /api
'volumeInfo'
W dniu piątek, 10 maja 2019 03:04:17 UTC+2 użytkownik Joe Reitman napisał:
>
> 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 [email protected].
To post to this group, send email to [email protected].
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/f4c3bc2f-4bd7-4098-aa2d-8716f67bdbb0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.