像我在 Jinja 模板中一样使用点获取字典值时在 Python 中得到 "there is no books attribute in the dict"

Got "there is no books attribute in the dict" in Python when use dot to get the dict value like I do in Jinja template

我在访问来自我的 http 请求的响应数据时遇到问题。如果我将响应传递给我的 html 然后从中取出我想要的特定数据,我就能得到它,但是如果我尝试在我的 python 中取出响应的相同部分文件并将其传递给我的 html 它说 "there is no books attribute in the dict".

我的html

{% extends "layout.html" %}

{% block heading %}
Search Page
{% endblock %}


{% block body %}

the result of the http request:
<p>  {{res}} </p>


I want to add this info from the request
<p>{{res.books[0].average_rating}}
{{res.books[0].work_ratings_count}}</p>


to this dictionary

{{apiDict}}

but the when I use the same syntax to access the average rating and ratings count 
from 
'res' in my python file it says the respose has no book object, why does this 
happen?

{% endblock %}

这是我的 python/flask 代码:

@app.route("/api/<isbn>", methods=["GET"])
def apiacc(isbn):
res = requests.get("https://www.goodreads.com/book/review_counts.json", params=. 
{"key": "lzhHXUd9kUpum244vufV2Q", "isbns": isbn}).json()
# avg = res.books[0].average_rating
# rc = res.books[0].work_ratings_count
book = db.execute("SELECT * FROM books WHERE isbn = :i", {"i": isbn}).fetchone()
db.commit()


apiDict = {
    "title": book.title,
    "author": book.author,
    "year": book.year,
    "isbn": isbn
}
# apiDict["average_score"] = res.books[0].average_rating
# apiDict["review_count"] = res.books[0].work_ratings_count

return render_template("api.html", res = res, apiDict=apiDict)

我想要这样的 python 代码:

 apiDict = {
    "title": book.title,
    "author": book.author,
    "year": book.year,
    "isbn": isbn,
    "average_score": avg,
    "review_count": rc
 }

并将 apiDict 作为唯一值传递给 api.hmtl 但我得到了我之前提到的错误。enter image description here

requests返回的res是一个dict。在模板中,Jinja 支持使用点运算符获取字典值,如:

{{ res.books }}

但在Python中,你必须使用括号运算符来获取dict中的值(点运算符用于获取属性):

data = res['books']