猿问

jinja2.exceptions.UndefinedError: 'dict object'

这是我的代码,谁调用错误:


    {% extends "add_book_header.html" %}



{% block content %}

    {% if books %}

        <p>Results: {{ books|length }}</p>

        {% for book in books %}

            <div class="card h-100">

                <img src={{book["volumeInfo"]["imageLinks"]["smallThumbnail"]}} class="card-img-top">

                <div class="card-body">

                    <h5 class="card-title">{{ book["volumeInfo"]["title"]}}</h5>

                    <p class="card-text">descripthion</p>

                    <a href="#" class="btn btn-primary">link</a>

                </div>

            </div>

        {% endfor %}

    {% endif %}

{% endblock %}

这是书的结构


    {

      "kind": "books#volume",

      "id": "NwxaDwAAQBAJ",

      "etag": "guYzwBLXoHI",

      "selfLink": "https://www.googleapis.com/books/v1/volumes/NwxaDwAAQBAJ",

      "volumeInfo": {

        "title": "Цельная жизнь. Главные навыки для достижения ваших целей",

        "authors": [

          "Джек Кэнфилд",

          "Марк Виктор Хансен",

          "Лес Хьюитт"

        ],

        "publisher": "Litres",

        "publishedDate": "2020-01-31",

        "industryIdentifiers": [

          {

            "type": "ISBN_10",

            "identifier": "5041133891",

          },

          {

            "type": "ISBN_13",

            "identifier": "9785041133894",

          }

        ],

        "readingModes": {

          "text": true,

          "image": true,

        },


你可以相信我,所有的钥匙都是对的。这是我的错误:


jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'imageLinks'

我已经为这个错误做了很多解决方案,我发现了。但什么都没奏效。包括它:


{% set img = book["volumeInfo"]["imageLinks"] %}

        ...

        <img src={{img["smallThumbnail"]}} class="card-img-top">

第一个字符串效果很好,但第二个是调用错误。拜托我需要你的帮忙


慕标琳琳
浏览 380回答 3
3回答

沧海一幻觉

您没有考虑书籍对象中的项目键{% block content %}&nbsp; &nbsp; {% if books %}&nbsp; &nbsp; &nbsp; &nbsp; <p>Results: {{ books|length }}</p>&nbsp; &nbsp; &nbsp; &nbsp; {% for book in books %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card h-100">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for item in book['items'] %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src={{item["volumeInfo"]["imageLinks"]["smallThumbnail"]}} class="card-img-top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="card-title">{{ item["volumeInfo"]["title"]}}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="card-text">descripthion</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" class="btn btn-primary">link</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; {% endif %}{% endblock %}

杨__羊羊

Privet Vladimir,我看到您对我的第一个答案的评论并删除了第一个答案。似乎 Jinja 对象内的内部字典未正确解析,但我无法在我的计算机上重现您的错误。也许嵌套的层次太多了。为了让它在我的计算机上工作,我必须纠正的一件事是那些奇怪的真假出现在没有引号或大写字母的响应中。也许这是这个问题的副作用。我可以想象的另一个选择是,首先在您的控制器中解析来自 googleapis 的响应,并在第一级构建一个包含所有需要字段的书籍对象,然后将此书籍对象传递给 jinja 模板。希望这可以帮助

MMTTMM

您始终必须使用以下方式访问 jinja&nbsp;".":-{{book["volumeInfo"].imageLinks.smallThumbnail"}}如果它的数组使用'',如果它的嵌套对象使用.&nbsp;,但如果你想在 .py 文件中使用,使用''一个。
随时随地看视频慕课网APP

相关分类

Python
我要回答