Razik Razik - 2 months ago 12x
HTML Question

Why get TypeError: 'function' object is not iterable?

I don't know whats wrong with this code, but get "TypeError: 'function' object is not iterable"
It's properly works for '/filestream' but not for '/allfile'

python code

def allfile():
fileStream = models.File.select().limit(100)
return render_template('filestream.html', filestream=filestream)

def filestream(username=None):
template = 'fileStream.html'
if username and username != current_user.username:
user = models.User.select().where(models.User.username**username).get() # the ** is the "like" operator (non-case sensitive comparison)
filestream = user.files.limit(100)
filestream = current_user.get_filestream().limit(100)
user = current_user
#if username:
#template = 'user_stream.html'
return render_template(template, filestream=filestream, user=user)

HTML code

{% extends "layout.html" %}

{% block content %}
{% for file in filestream %}
<a href="{{ url_for('filestream', username=file.user.username) }}">
{{ file.user.username }}
<i class="clock"></i>
<time data-time="{{ file.timestamp }}" }}">
{{ file.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}
<!-- <a href="{{ url_for('view_file', file_id=file.id) }}" class="view">View</a>-->
<div class="file">
<a href="{{ file.path}}"</a>
<img src="{{ file.path }}" alt="" style="width:521px;height:512px;">
{% endfor %}
{% endblock %}


Inside allfile() you define a local variable called fileStream, with a capital S. But what you pass to the template is filestream, with lower-case S; this isn't defined locally, so Python uses the module-level name which refers to the filestream() handler.

Make sure you're using consistent capitalization in your functions.