【问题标题】:TypeError: 'Key' object is not iterableTypeError:“键”对象不可迭代
【发布时间】:2015-12-14 09:10:08
【问题描述】:

应用程序 - 托管在谷歌应用引擎上的 Flask 应用程序。

我在尝试渲染模板时收到以下错误

  File "Show_Messages.html", line 1, in top-level template code
    {% extends "AdminMaster.html" %}
TypeError: 'Key' object is not iterable

我的模板(Show_messages.html)

{% extends "AdminMaster.html" %}
{% block title %}News{% endblock %}
{% block page %}News{% endblock %}
{% block head %}
    {{ super() }}
{% endblock %}
{% block content %}
    <div class="container">
        <div class="page-header">
            <h1>News</h1>
        </div>
    <div class="col-lg-12">
        <ul class="list-group">
            {% for newsitem in newsitems %}
                <li class="list-group-item">
                    <span class="badge">
                        <a href="Edit-NewsItem?ID={{ newsitem.key.urlsafe() }}">
                            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                        </a>
                    </span>
                </li>
            {% endfor %}
        </ul>
    </div>
    </div>
{% endblock %}

路由函数

@message_admin_routes.route('/xxxx/xxxx-xxxx')
@authenticate_admin
def show_messages():
    breadcrumb, user = build_user_and_breadcrumbs()
    messages = MessageFactory().get_messages_key()
    return render_template('Show_Messages.html',
                           user=user,
                           breadcrumb=breadcrumb,
                           newsitems=messages)

--------已编辑------

AdminMaster.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<!--googleoff: snippet-->
{% block head %}
    <title>CFC Melbourne - {% block title %}{%endblock%}</title>
    <meta name="Section" content="{% block page %}{%endblock%}">
    <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://storage.googleapis.com/xxxx/xxx/xxx.css" type="text/css">
    <link rel="stylesheet" href="https://storage.googleapis.com/xxxx/xxxx/xxxx.css" type="text/css">
  <link rel="shortcut icon" href="https://storage.googleapis.com/xxxx/xxxx/favicon.ico">
    <link href='https://storage.googleapis.com/xxx/xxx.css' rel='stylesheet' type='text/css'>
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">
{% endblock %}
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script>
        /**
        * Function that tracks a click on an outbound link in Google Analytics.
        * This function takes a valid URL string as an argument, and uses that URL string
        * as the event label.
        */
        var trackOutboundLink = function(url) {
           ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
             function () {
             document.location = url;
             }
           });
        }
    </script>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'xxx-xx-xx', 'xxx-xx-xx.xxx.xxx');
      ga('send', 'pageview');
    </script>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div id="wrap">
    <div id="menu">
        <nav class="navbar navbar-default" style="margin-bottom: 0px;">
            <div class="container-fluid">
                   <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/Eden"><div class='logo-text-a' style="color: black">CFC Melbourne Admin</div></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Applications <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="/Eden/Applications">List all applications</a></li>
            <li><a href="/Eden/New-Application">Create new application</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="/Eden/Vision">Vision</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated linkkk</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ user.nickname() }}<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Permissions</a></li>
            <li><a href="#">User Information</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="{{ logout }}">Logout</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
            </div>
        </nav>
        {% include 'AdminBreadcrumb.html' %}
    </div>

    <div id="content">
        {% block content %}{% endblock %}
    </div>
</div>

<!--googleoff: snippet-->
{% block footer %}

    <script src="https://storage.googleapis.com/xxxx/js/jquery-2.1.0.min.js"></script>
    <script src="https://storage.googleapis.com/xxxx/js/bootstrap.min.js"></script>
        <script>
// Remove active for all items.
        $('.page-sidebar-menu li').removeClass('active');

        // highlight submenu item
        $('li a[href="' + this.location.pathname + '"]').parent().addClass('active');

        // Highlight parent menu item.
        $('ul a[href="' + this.location.pathname + '"]').parents('li').addClass('active');
    </script>
{% endblock%}
</body>
</html>

【问题讨论】:

  • AdminMaster.html 中有什么?
  • 错误可能仍然在您的AdminMaster.html 模板中..
  • MessageFactory().get_messages_key() 是做什么的?
  • 堆栈跟踪不能帮助我归零。我已经添加了AdminMaster.html的代码
  • 你找到我了。我调用了错误的函数!!!哎呀

标签: python flask


【解决方案1】:

在您的模板代码中,您有一个 for 循环,它需要一个像列表这样的可迭代对象。但是您在此处传递的某些项目是不可迭代的。 您需要在循环之前检查该项目是否可迭代。下面给出一个例子;

<ul class="sitemap">
{%- for item in sitemap recursive %}    
    <li><a href="{{ item.href|e }}">{{ item.title }}</a>
    {%- if item.children -%}
        <ul class="submenu">{{ loop(item.children) }}</ul>
    {%- endif %}</li>
{%- endfor %}
</ul>

更多详情可以在这里找到:https://stackoverflow.com/a/21006895/2336603

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2017-08-27
    • 2018-10-10
    • 2021-12-13
    • 2019-02-20
    • 2020-03-27
    • 2018-12-12
    • 2018-07-16
    • 2011-09-12
    相关资源
    最近更新 更多