Forums

TypeError: 'Pagination' object is not iterable

This is more a question about Flask SQLAlchemy than pythonanywhere, but this error does not occur on my local machine.

I was able to get my website up and running but am getting an error when trying to access one particular route:

@app.route("/")
def home():
    page = request.args.get("page", type=int)
    per_page = session.get("per_page") or 6
    posts_paginated = Post.query.order_by(Post.date_posted.desc()).paginate(per_page=per_page, page=page)
    if page:
        posts_paginated.page = min(posts_paginated.pages, page)
    return render_template("index.html", posts_paginated=posts_paginated)

Error log shows:

  File "/home/emannomad/flask-blog/templates/index.html", line 8, in block 'content'
    {% for post in posts_paginated %}
TypeError: 'Pagination' object is not iterable

Pagination object is supposed to be iterable and it works on my local machine.

Have a look at the docs, you should probably use the iter_pages method if you want to loop over it in the template (as per the docs example: {%- for page in pagination.iter_pages() %}).