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**
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.