google app engine - How to flip to previous page with ndb cursors? -


i cant manage 'previous page' in ndb paging.

i have checked documentation , similar question here without success.

 def show_feedback(kind, bookmark=none):     """renders returned feedback."""     cursor = none         more_p= none     if bookmark:         cursor = cursor(urlsafe=bookmark)      q = feedback.query()     q_forward = q.filter(feedback.kind==feedback.kinds[kind]).order(-feedback.pub_date)     q_reverse = q.filter(feedback.kind==feedback.kinds[kind]).order(feedback.pub_date)      feedbacks, next_cursor, more = q_forward.fetch_page(app.config['feedback_per_page'], start_cursor=cursor)     if cursor:         rev_cursor = cursor.reversed()         feedbacks2, prev_cursor, more_p = q_reverse.fetch_page(app.config['feedback_per_page'], start_cursor=rev_cursor)      next_bookmark = none     prev_bookmark = none     if more , next_cursor:         next_bookmark = next_cursor.urlsafe()     if more_p , prev_cursor:         prev_bookmark = prev_cursor.urlsafe()     return render_template_f11('show_feedback.html', kind=kind, feedbacks=feedbacks, next_bookmark=next_bookmark, prev_bookmark=prev_bookmark) 

html:

  {% if prev_bookmark %}         <a href="{{ url_for(request.endpoint, bookmark=prev_bookmark) }}">previous</a>   {% endif %}   {% if next_bookmark %}     <a href="{{ url_for(request.endpoint, bookmark=next_bookmark) }}">next</a>   {% endif %} 

i can page forwards correctly until end. can't page backwards until last page , can't page until first page neither.

what missing please?

update:

changed code faisal's suggestions. works better must admit. still paging doesn't work correctly:

i have 7 entries. page_size in config 3. hence 3 pages:

when clicking on next 7,6,5 -> 4,3,2 -> 1 perfect. when clicking on previous: 1 -> 3,4,5 (?) -> 5,6,7 (?)

thanks help

def show_feedback(kind, bookmark=none):     """renders returned feedback."""     is_prev = request.args.get('prev', false)     cursor = none             if bookmark:         cursor = cursor(urlsafe=bookmark)      q = feedback.query()     q_forward = q.filter(feedback.kind==feedback.kinds[kind]).order(-feedback.pub_date)     q_reverse = q.filter(feedback.kind==feedback.kinds[kind]).order(feedback.pub_date)      qry = q_reverse if is_prev else q_forward      feedbacks, cursor, more = qry.fetch_page(app.config['feedback_per_page'], start_cursor=cursor)      if is_prev:         prev_bookmark = cursor.reversed().urlsafe() if more else none         next_bookmark = bookmark     else:         prev_bookmark = bookmark         next_bookmark = cursor.urlsafe() if more else none     return render_template_f11('show_feedback.html', kind=kind, feedbacks=feedbacks, next_bookmark=next_bookmark, prev_bookmark=prev_bookmark) 

update 2:

it seems working reverse().

7,6,5 -> next -> 4,3,2 -> next -> 1

1 -> prev -> 2,3,4 -> 5,6,7 (order no longr latest date first)

so here use current bookmark navigating next or previous , removed other query doesn't query twice each request. (edited old description/answer wrong when tested it. 1 works on localhost).

try:

is_prev = self.request.get('prev', false) if is_prev:     qry = q_reverse     cursor = cursor.reversed() else:     qry = q_forward  feedbacks, cursor, more = qry.fetch_page(app.config['feedback_per_page'], start_cursor=cursor)  if is_prev:     prev_bookmark = cursor.reversed().urlsafe() if more else none     next_bookmark = bookmark else:     prev_bookmark = bookmark     next_bookmark = cursor.urlsafe() if more else none 

html

{% if prev_bookmark %}     <a href="{{ url_for(request.endpoint, bookmark=prev_bookmark, prev=true) }}">previous</a> {% endif %} {% if next_bookmark %}   <a href="{{ url_for(request.endpoint, bookmark=next_bookmark) }}">next</a> {% endif %} 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -