TypeError:“函数”对象不是可迭代的烧瓶

时间:2019-10-01 15:59:13

标签: python flask

我正在Flask中测试一个新的应用程序,但我不知道出了什么问题。

我创建了 toy.py ,并向其中添加了一个类。

class Toy:
    count = 1

    def __init__(self, name):
        self.name = name
        self.id = Toy.count
        Toy.count += 1

app.py

from flask import Flask, render_template, url_for, request, redirect
from toy import Toy


app = Flask(__name__)


spiderman = Toy(name='spiderman')
superman = Toy(name='superman')
bathman = Toy(name='bathman')

toys = [spiderman, superman, bathman]

@app.route('/toys',)
def toys():
    return render_template('toys.html', toys=toys)


if __name__ == '__main__':
    app.run(debug=True)

在HTML文件中,我试图循环为真

{% extends 'base.html' %}

{% block title %}
TOYS PAGE
{% endblock %}

{% block body %}
<ul>
    {% for toy in toys %}
    <li>{{ toy.name }}</li>
    {% endfor %}
</ul>

{% endblock %}

我收到以下错误:

**{% for toy in toys %}
TypeError: 'function' object is not iterable**

1 个答案:

答案 0 :(得分:1)

您正在重新定义名称const data = [ { Date: "Tue Oct 01 2019" }, { Date: "Tue Oct 01 2019" }, { Date: "Tue Oct 01 2019" }, { Date: "Tue Oct 02 2019" } ]; const result = data.reduce((total, value) => { total[value.Date] = (total[value.Date] || 0) + 1; return total; }, {}); console.log(result);

先入为主,

toys

第二次

toys = [spiderman, superman, bathman]

因此,@app.route('/toys',) def toys(): return render_template('toys.html', toys=toys) 最后是一个函数。也许将toys列表命名为Toy