TypeError: объект 'function' не является итеративной колбой - PullRequest
0 голосов
/ 01 октября 2019

Я тестирую новое приложение во 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 Ответ

1 голос
/ 01 октября 2019

Вы переопределяете имя toys.

Первый в

toys = [spiderman, superman, bathman]

Второй в

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

Итак, в конце toys - этофункция. Возможно назовите список Toy как _toys

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...