librelist archives

« back to archive

Pagination problems

Pagination problems

From:
Faryus
Date:
2015-06-15 @ 09:20
Hello everyone,

I'm having a bit of trouble with implementing pagination on a project.

I am using this snippet here: http://flask.pocoo.org/snippets/44/

I have that code in a pagination.py file, which I import into my app.py 
file.

My test database contains 11 items, and I am trying to display 5 on each 
page.

My code looks like this, currently:

    PER_PAGE = 5

    @app.route("/list/", defaults{"page":1})
    @app.route("/list/<page:int>")
    def list_items(page):
         cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
         items = [dict(id=row[0], location=row[1], item=row[2]) for row
    in cur.fetchall()]
         count = len(items)
         pagination = Pagination(page, PER_PAGE, count)
         return render_temaplte("list.html", items=items,
    pagination=pagination)


I get no errors while running the code, but it doesn't list 5 items for 
each page, it continues to list all the items.

What am I doing wrong here?

Many thanks,
_Alex C.

Re: [flask] Pagination problems

From:
Faryus
Date:
2015-06-16 @ 14:14
Thank you all, I managed to solve it with your help.

On 15.06.2015 12:20, Faryus wrote:
> Hello everyone,
>
> I'm having a bit of trouble with implementing pagination on a project.
>
> I am using this snippet here: http://flask.pocoo.org/snippets/44/
>
> I have that code in a pagination.py file, which I import into my 
> app.py file.
>
> My test database contains 11 items, and I am trying to display 5 on 
> each page.
>
> My code looks like this, currently:
>
>     PER_PAGE = 5
>
>     @app.route("/list/", defaults{"page":1})
>     @app.route("/list/<page:int>")
>     def list_items(page):
>         cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
>         items = [dict(id=row[0], location=row[1], item=row[2]) for row
>     in cur.fetchall()]
>         count = len(items)
>         pagination = Pagination(page, PER_PAGE, count)
>         return render_temaplte("list.html", items=items,
>     pagination=pagination)
>
>
> I get no errors while running the code, but it doesn't list 5 items 
> for each page, it continues to list all the items.
>
> What am I doing wrong here?
>
> Many thanks,
> _Alex C.

Re: [flask] Pagination problems

From:
Sudheer Satyanarayana
Date:
2015-06-15 @ 11:59

On 06/15/2015 02:50 PM, Faryus wrote:
> Hello everyone,
>
> I'm having a bit of trouble with implementing pagination on a project.
>
> I am using this snippet here: http://flask.pocoo.org/snippets/44/
FYI: There's Flask-Webhelpers.
Docs at 
https://github.com/bngsudheer/Flask-Webhelpers/blob/master/docs/source/index.rst

--
Sudheer

Re: [flask] Pagination problems

From:
Matt Smith
Date:
2015-06-15 @ 09:29
The pagination is only to handle the page numbers, you need to handle the
items yourself.
something like this would work.
return render_temaplte("list.html", items=items[page*PER_PAGE:(
page+1)*PER_PAGE], pagination=pagination)


On Mon, 15 Jun 2015 at 10:24 Faryus <faryus@cock.li> wrote:

>  Hello everyone,
>
> I'm having a bit of trouble with implementing pagination on a project.
>
> I am using this snippet here: http://flask.pocoo.org/snippets/44/
>
> I have that code in a pagination.py file, which I import into my app.py
> file.
>
> My test database contains 11 items, and I am trying to display 5 on each
> page.
>
> My code looks like this, currently:
>
>  PER_PAGE = 5
>
> @app.route("/list/", defaults{"page":1})
> @app.route("/list/<page:int>")
> def list_items(page):
>     cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
>     items = [dict(id=row[0], location=row[1], item=row[2]) for row in
> cur.fetchall()]
>     count = len(items)
>     pagination = Pagination(page, PER_PAGE, count)
>     return render_temaplte("list.html", items=items, pagination=pagination)
>
>
> I get no errors while running the code, but it doesn't list 5 items for
> each page, it continues to list all the items.
>
> What am I doing wrong here?
>
> Many thanks,
> _Alex C.
>
-- 

Matt Smith, Riot Games.

Re: [flask] Pagination problems

From:
Kunet Internet Services Zimbabwe
Date:
2015-06-15 @ 10:48
I think you can limit the number of results returned by the query.

change this line

     cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
to
      cur = g.db.execute("SELECT * FROM testdb ORDER BY id LIMIT 5 OFFSET 0")

This will return the first five items.

to return the second set multiply page number by five and the set the
results  as
the offset in the database query string. So you might need to build
the query string first.

querystring = "SELECT * FROM testdb ORDER BY id LIMIT 5 OFFSET
{}".format(offset)



On 6/15/15, Matt Smith <msmith@riotgames.com> wrote:
> The pagination is only to handle the page numbers, you need to handle the
> items yourself.
> something like this would work.
> return render_temaplte("list.html", items=items[page*PER_PAGE:(
> page+1)*PER_PAGE], pagination=pagination)
>
>
> On Mon, 15 Jun 2015 at 10:24 Faryus <faryus@cock.li> wrote:
>
>>  Hello everyone,
>>
>> I'm having a bit of trouble with implementing pagination on a project.
>>
>> I am using this snippet here: http://flask.pocoo.org/snippets/44/
>>
>> I have that code in a pagination.py file, which I import into my app.py
>> file.
>>
>> My test database contains 11 items, and I am trying to display 5 on each
>> page.
>>
>> My code looks like this, currently:
>>
>>  PER_PAGE = 5
>>
>> @app.route("/list/", defaults{"page":1})
>> @app.route("/list/<page:int>")
>> def list_items(page):
>>     cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
>>     items = [dict(id=row[0], location=row[1], item=row[2]) for row in
>> cur.fetchall()]
>>     count = len(items)
>>     pagination = Pagination(page, PER_PAGE, count)
>>     return render_temaplte("list.html", items=items,
>> pagination=pagination)
>>
>>
>> I get no errors while running the code, but it doesn't list 5 items for
>> each page, it continues to list all the items.
>>
>> What am I doing wrong here?
>>
>> Many thanks,
>> _Alex C.
>>
> --
>
> Matt Smith, Riot Games.
>

Re: [flask] Pagination problems

From:
Matt Smith
Date:
2015-06-15 @ 11:33
If you limit the query you'll need to run another query to get the total
count of items, pagination requires a total count to be effective.

On Mon, 15 Jun 2015 at 11:52 Kunet Internet Services Zimbabwe <
kunet.co.zw@gmail.com> wrote:

> I think you can limit the number of results returned by the query.
>
> change this line
>
>      cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
> to
>       cur = g.db.execute("SELECT * FROM testdb ORDER BY id LIMIT 5 OFFSET
> 0")
>
> This will return the first five items.
>
> to return the second set multiply page number by five and the set the
> results  as
> the offset in the database query string. So you might need to build
> the query string first.
>
> querystring = "SELECT * FROM testdb ORDER BY id LIMIT 5 OFFSET
> {}".format(offset)
>
>
>
> On 6/15/15, Matt Smith <msmith@riotgames.com> wrote:
> > The pagination is only to handle the page numbers, you need to handle the
> > items yourself.
> > something like this would work.
> > return render_temaplte("list.html", items=items[page*PER_PAGE:(
> > page+1)*PER_PAGE], pagination=pagination)
> >
> >
> > On Mon, 15 Jun 2015 at 10:24 Faryus <faryus@cock.li> wrote:
> >
> >>  Hello everyone,
> >>
> >> I'm having a bit of trouble with implementing pagination on a project.
> >>
> >> I am using this snippet here: http://flask.pocoo.org/snippets/44/
> >>
> >> I have that code in a pagination.py file, which I import into my app.py
> >> file.
> >>
> >> My test database contains 11 items, and I am trying to display 5 on each
> >> page.
> >>
> >> My code looks like this, currently:
> >>
> >>  PER_PAGE = 5
> >>
> >> @app.route("/list/", defaults{"page":1})
> >> @app.route("/list/<page:int>")
> >> def list_items(page):
> >>     cur = g.db.execute("SELECT * FROM testdb ORDER BY id")
> >>     items = [dict(id=row[0], location=row[1], item=row[2]) for row in
> >> cur.fetchall()]
> >>     count = len(items)
> >>     pagination = Pagination(page, PER_PAGE, count)
> >>     return render_temaplte("list.html", items=items,
> >> pagination=pagination)
> >>
> >>
> >> I get no errors while running the code, but it doesn't list 5 items for
> >> each page, it continues to list all the items.
> >>
> >> What am I doing wrong here?
> >>
> >> Many thanks,
> >> _Alex C.
> >>
> > --
> >
> > Matt Smith, Riot Games.
> >
>
-- 

Matt Smith, Riot Games.