3

I'm trying to paginate the results of a query in Flask using Peewee. I'm trying to use the function object_list from playhouse.flask_utils.

You can see the example of object_list seems pretty straightforward and the following code is the one I'm trying to make work:

from flask import Flask
from playhouse.flask_utils import object_list
from peewee import *
from database import *

@app.route("/items/")                                                         
def items():                                                                  
    all_items = Quote.select()                                                     
    return object_list("items.html", all_items) 

And the template items.html contains the following:

<p>Hello world</p>                                               
{% for item in all_items %}                                                       
    <p>{{ item.text }}</p>                                                    
{% endfor %}                                                                    

{% if page > 1 %}                                                               
    <a class="previous" href="./?page={{ page - 1 }}">Previous</a>              
{% endif %}                                                                     
{% if pagination.get_pages() > page %}                                          
    <a class="next" href="./?page={{ page + 1 }}">Next</a>                      
{% endif %}        

And of course, I run python app.py but I receive a 500 Internal Server Error. I'm interested in paginating the results of my query, any idea how can I achieve this? or can you spot what I'm doing wrong here? thank you in advance.

Edit #1, The following error appears in the terminal:

127.0.0.1 - - [09/Jan/2016 20:47:17] "GET /items/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/web/index.py", line 19, in items
    return object_list("items.html", all_items)
  File "/usr/lib/python2.7/site-packages/playhouse/flask_utils.py", line 64, in object_list
    **kwargs)
  File "/usr/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/usr/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/web/templates/items.html", line 9, in top-level template code
    {% if pagination.get_pages() > page %}
UndefinedError: 'playhouse.flask_utils.PaginatedQuery object' has no attribute 'get_pages'
gglasses
  • 826
  • 11
  • 30
  • What's your error message? – Eli Rose Jan 10 '16 at 02:20
  • @EliRose thank you for your interest. I edited my question and added the error message that appears in the terminal! – gglasses Jan 10 '16 at 02:24
  • The Python traceback will be more helpful -- it should be displayed in your browser when you visit the page. Is that happening? – Eli Rose Jan 10 '16 at 02:25
  • @EliRose Nope, it isn't happening. No traceback is appearing. – gglasses Jan 10 '16 at 02:29
  • Is debug on? If it is, you should get something that looks like the screen http://flask.pocoo.org/docs/0.10/quickstart/. If it's not, turn it on. – Eli Rose Jan 10 '16 at 02:32
  • @EliRose thank you for that. I edited the question and added the traceback. The error says the following: `UndefinedError: 'playhouse.flask_utils.PaginatedQuery object' has no attribute 'get_pages'`. – gglasses Jan 10 '16 at 02:51

1 Answers1

9

There's no get_pages() method. Use: get_page(x) to get a page of results. Use get_page_count() to get the total number of pages.

This is all documented here: http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#PaginatedQuery

coleifer
  • 24,887
  • 6
  • 60
  • 75
  • 5
    I thought I've seen `coleifer` somewhere else and then I remember the url of `peewee` on github: https://github.com/coleifer/peewee :) – gglasses Jan 17 '16 at 03:31