1

I'm testing a new app in Flask but I don't know what went wrong.

I created toy.py and added a class to it.

class Toy:
    count = 1

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

In the 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)

In the HTML file I was trying to loop true

{% extends 'base.html' %}

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

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

{% endblock %}

I am receiving the following error:

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

1 Answer 1

4

You are redefining the name toys.

First in,

toys = [spiderman, superman, bathman]

Second in,

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

So in the end toys is a function. Maybe name the Toy list as _toys

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.