Reputation: 1013
I have a table with data(s). I want to get the result set with pagination in API response.
I am using the GET
method to call API. Here is the
Requirement:
My Code :
@api.route('/', methods=["GET"])
@app.route('/page/<int:page>')
class List(Resource):
"""USER data(s)"""
def get(self, page=1):
"""GET Lists"""
all_data = User.query.paginate(page, per_page=2)
result = user_serializer.dump(all_data)
return result
Issue:
TypeError: 'Pagination' object is not iterable in Flask
Upvotes: 1
Views: 6665
Reputation: 171
I added .items
in the for loop of the jinja template and it worked.
Example
{% extends "layout.html" %}
{% block content %}
{% for user in users.items %}
<article class="media content-section mt-4">
<img class="rounded-circle" article-img src="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ user.first_name }}</a>
<small class="text-muted"></small>
</div>
<h2><a class="article-title" href=""></a></h2>
<p class="article-content">
</p>
</div>
</article>
{% endfor %}
{% endblock content %}
Upvotes: 2
Reputation: 354
The issue with your code is you are trying to pass the Pagination
object to your serializer
. Instead what the serializer expects is either a list of model instances or a single model instance. Just change your call to this
result = user_serializer.dump(all_data.items)
Sources
Upvotes: 6