-1
I’m reading a JSON that looks like this: Google Books and trying to display it in HTML. The user’s search input is the seek
which is received through the request.form.get
down below:
import requests
from flask import Flask, render_template, redirect, request, session
@app.route('/search', methods=["GET", "POST"])
@login_required
def search():
"""Pesquisa um livro utilizando a API do Google Books"""
if request.method == "POST":
try:
seek = request.form.get("seek")
url = f'https://www.googleapis.com/books/v1/volumes?q={seek}'
response = requests.get(url)
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
search = response.json()
search = {
"totalItems": int(search["totalItems"]),
"items": search["items"]
}
return render_template("search.html", search=search)
except (KeyError, TypeError, ValueError):
return None
else:
return render_template("index.html")
The search works well, but I still don’t know where I’m going wrong when sending to HTML. I’m trying to display it as follows:
{% for seek in search %}
<figure>
<a><img src="{{ seek['items']['imageLinks']['thumbnail'] }}"></a>
<p>{{ seek["items"]["volumeInfo"]["title"] }}</p>
<p>{{ seek["items"]["volumeInfo"]["authors"] }}</p>
</figure>
{% endfor %}
The terminal returns me the following message:
jinja2.exceptions.UndefinedError: 'str object' has no attribute 'items'
Someone would know to tell me what I’m missing?
Very good your solution Fernando! However, some of the research is returning
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'imageLinks'
. By the terms that I’ve been researching, I found that some books do not have thumbnail, you can tell me what would be the exit in this case?– Arnon
Fixed... Seek in this syntax loads the array’s input
– Fernando Favini