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
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
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 %}
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.