pagination issue redirecting to the current page

286 views
Skip to first unread message

Joe

unread,
Nov 14, 2014, 8:37:25 PM11/14/14
to web...@googlegroups.com
I am working on an app where the user can select items by clicking on a button for each item on the index page. It works fine, except after each item selected the user has to be redirected to index and ends up on the top of the first page at item 1. Then the user has to go back each time and try to find whatever page they were on before when they made the last selection. How can I fix this so when the user selects the item, he is redirected to the same page?

def index():
    if len(request.args):
        page=int(request.args[0])
    else:
        page=0
    items_per_page=11
    limitby=(page*items_per_page,(page+1)*items_per_page + 1)
    rows=db().select(db.post.ALL, limitby=limitby)
    form = SQLFORM(db.post)
    if form.process().accepted:
        redirect()
    selected = db(db.post.selected == True).select()
    not_selected = db(db.post.selected == False).select()
    return dict(form=form, selected=selected, not_selected=not_selected, rows=rows, page=page, limitby=limitby,items_per_page=items_per_page)

def select():
    id = request.vars.id
    name = db(db.post.id == id).select().first()
    if name:
        name.selected = not name.selected
        name.update_record()
    return redirect(URL('index'))

Cliff Kachinske

unread,
Nov 17, 2014, 11:37:09 PM11/17/14
to web...@googlegroups.com
Put information about the current page in the request.args dictionary. You could use the session, but if your user opens a second browser window it becomes difficult to keep track of which session data corresponds to which browser tab.

Joe

unread,
Nov 17, 2014, 11:59:16 PM11/17/14
to web...@googlegroups.com
Thanks Cliff, I've been trying to do that but I didn't succeed. I would appreciate if you could send an example on how to put the current page info in the request.args dictionary.

Massimo Di Pierro

unread,
Nov 19, 2014, 10:38:05 PM11/19/14
to web...@googlegroups.com
URL('index',args=(2)) will give you page #2.

Joe

unread,
Nov 19, 2014, 10:59:11 PM11/19/14
to web...@googlegroups.com
@Massimo, Thanks very much Massimo. I am trying to find a way to redirect the user to the page where the user clicks on the select button to select an item. The user might be on page 2 or page 3 page 4 or any other page when he selects the item. How can I redirect the user to the same page he is on when making the selection.

This is the View for the Controller code I posted:

{{extend 'layout.html'}}
{{for i in rows:}}
<div class="well">
<h5>{{=i.name}}</h5>
{{if not i.selected:}}
{{=A('Select',_class="btn btn-danger",_href=URL("select",args=i.id, vars=dict(id=i.id)))}}
{{elif i.selected:}}
{{=A('Selected',_class="btn btn-info",_href=URL("select",args=i.id, vars=dict(id=i.id)))}}
{{pass}}
</div>
{{pass}}
<br><br>
{{if page:}}
<a href="{{=URL(r=request, args=[page-1])}}">previous</a>
{{pass}}
{{if len(rows)>=items_per_page:}}
<a href="{{=URL(r=request, args=[page+1])}}">next</a>
{{pass}}

Michele Comitini

unread,
Jan 2, 2015, 9:18:44 AM1/2/15
to web...@googlegroups.com
in the view links add the page and element id:

{{=A('Select', _id = i.id, _class="btn btn-danger",_href=URL("select", args=i.id, vars=dict(id=i.id,page=page)))}}

{{elif i.selected:}}
{{=A('Selected', _id = i.id, _class="btn btn-info",_href=URL("select", args=i.id, vars=dict(id=i.id, page=page)))}}


in action select() do:
redirect(URL('index#' + request.vars.id, args=[request.vars.page]))

no return needed!

Actually you could resort to using AJAX for instance by using
{{=A('Selected', _id = i.id, _onclick="jQuery.get(" +URL('select', args=[i.id]).... ...
and use jQuery itself to manipulate the css class of the button itself

Joe

unread,
Jan 3, 2015, 4:43:13 PM1/3/15
to web...@googlegroups.com
Hi Michele, thanks so much for looking at this problem. I appreciate it very much. I tried the code but after selecting, the user is still sent back to page 1 each time, not staying on the same page.

Michele Comitini

unread,
Jan 4, 2015, 5:37:54 PM1/4/15
to web...@googlegroups.com
Joe,

If you stay on page 1, it's because you do not pass the args parameter correctly, when doing the redirect.
check you pass the variable page to the select() function and also that when you redirect back to index you pass correctly the page as request.args(0)

also simplify your code by removing the select action and by putting in the beginning of the index action:

  id = request.vars.id
  if id:
    name = db(db.post.id==id).update(selected=(db.post.selected==False))

and in the view:
 
{{=A('Select', _id = i.id, _class="btn btn-danger",_href=URL("index#%s" % i.id, args=[page], vars=dict(id=i.id,page=page)))}}

{{elif i.selected:}}
{{=A('Selected', _id = i.id, _class="btn btn-info",_href=URL("index#%s" % i.id, args=[page], vars=dict(id=i.id, page=page)))}}

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe

unread,
Jan 6, 2015, 8:48:01 AM1/6/15
to web...@googlegroups.com
Michele,

Thanks so much for this.

I tried to remove the select action and change the view as suggested but that wasn't working. So, I experimented with the redirect from the select action
to the index a couple of times.

I removed the request.vars.id, from the redirect and now it's working! The redirect looks like this now:

redirect(URL('index', args=[request.vars.page]))

So it's working! Thanks again very much! I really appreciate it.
Reply all
Reply to author
Forward
0 new messages