I'm having dict which can be seen here https://jsoneditoronline.org/?id=a570322381fc45919a7a03ebad78cbee
but from what I was reading so far, it's more likely a list of dicts. I am trying to display data from dicts like title, description, authors and categories but template tags are not displaying this what I want, tried with loops but can't access it
Tried loops like this
{% for items in books %}
{% for volumeInfo in items %}
{{ volumeInfo.title }}
{% endfor %}
{% endfor %}
only what works is {{ books.items }} but it's giving me the whole json but I need only few values.
def api(request):
books = {}
if 'books' in request.GET:
books = request.GET['books']
url = 'https://www.googleapis.com/books/v1/volumes?q=%s' % books
response = requests.get(url)
books = response.json()
print(type(books))
with open("data_file.json", "w") as write_file:
json.dump(books, write_file)
return render(request, 'books/api.html', {'books': books})
{% load get_item from template_filters %}
{% block content %}
<h2>Google API</h2>
<form method="get">
<input type="text" name="book">
<button type="submit">search on google books api</button>
</form>
{% if books %}
<p>
{% for items in books %}
{% for volumeInfo in items %}
{{ volumeInfo.title }}
{% endfor %}
{% endfor %}
{{ books.items }}
</p>
{% endif %}
{% endblock %}
You need to follow the structure of the JSON that you have. Summarising, the data looks like this:
{
"items": [
{
"volumeInfo": {
"title": "Hobbit czyli Tam i z powrotem"
}
}
]
}
So, in the template:
{% for item in books.items %}
{{ item.volumeInfo.title }}
{% endfor %}