如何使用ndb游标翻转到上一页? [英] How to flip to previous page with ndb cursors?

查看:198
本文介绍了如何使用ndb游标翻转到上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法设法到达ndb分页中的上一页。



我已检查过文档以及此类似这里问题没有成功。

  def show_feedback(kind,bookmark = None):
Renders返回反馈。
cursor =无
more_p =无
如果书签:
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)

feedback,next_cursor,more = q_forward.fetch_page (app.config ['FEEDBACK_PER_PAGE'],start_cursor = cursor)
如果cursor:
rev_cursor = cursor.reversed()
feedback2,prev_cursor,more_p = q_reverse.fetch_page 'FEEDBACK_PER_PAGE'],start_cursor = rev_cursor)

next_bookmark = None
prev_bookmark = None
如果more和next_cursor:
next_bookmark = next_cursor.urlsafe b if more_p and prev_cursor:
prev_bookmark = prev_cursor.urlsafe()
return render_template_f11('show_feedback.html',kind = kind,reactions = feedback,next_bookmark = next_bookmark,prev_bookmark = prev_bookmark)

html:

  {%if prev_bookmark%} 
< a href ={{url_for(request.endpoint,bookmark = prev_bookmark)}}>上一页< / a&
{%endif%}
{%if next_bookmark%}
< a href ={{url_for(request.endpoint,bookmark = next_bookmark)}}>下一页< / a> ;
{%endif%}

我可以正确向前翻页,直到结束。
但是我不能倒退,直到最后一页,即使这样,我也不能回页,直到第一页。



我遗漏了什么?



UPDATE:



使用Faisal的建议更改了代码。它工作更好我必须承认。但仍然无法正常使用分页:



我有7个条目。因此我们得到三个页面:



当点击Next时,我得到7,6,5 - > 4,3,2 - > 1 Perfect。
现在点击上一个:1 - > 3,4,5(?) - > 5,6,7(?)



感谢您的帮助

  def show_feedback(kind,bookmark = None):
Render返回反馈。 $ b is_prev = request.args.get('prev',False)
cursor = None
如果书签:
cursor =游标(urlsafe =书签)

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

feedback,cursor,more = qry.fetch_page
$ b如果is_prev:
prev_bookmark = cursor.reversed()。urlsafe()更多其他无
next_bookmark = bookmark
else:
prev_bookmark = bookmark
next_bookmark = cursor.urlsafe()if more else None
return render_template_f11('show_feedback.html',kind = kind,reactions = feedback,next_bookmark = next_bookmark ,prev_bookmark = prev_bookmark)

UPDATE 2:
$ b

现在它似乎与reverse()几乎一起工作了。



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



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

解决方案

所以我在这里做的是使用当前的书签来导航下一个或上一个和删除其他查询,每次请求查询两次。 (编辑旧的描述/答案是错误的,当我测试它。这一个在我的localhost)。



尝试:


$ b b

  is_prev = self.request.get('prev',False)
if is_prev:
qry = q_reverse
cursor = cursor.reversed ()
else:
qry = q_forward

feedback,cursor,more = qry.fetch_page(app.config ['FEEDBACK_PER_PAGE'],start_cursor = cursor)

if is_prev:
prev_bookmark = cursor.reversed()。urlsafe()if more $ 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)}} >上一页< / a>
{%endif%}
{%if next_bookmark%}
< a href ={{url_for(request.endpoint,bookmark = next_bookmark)}}>下一页< / a> ;
{%endif%}


I cant manage to get to 'previous page' in ndb paging.

I have checked the documentation and also this 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 and next_cursor:
        next_bookmark = next_cursor.urlsafe()
    if more_p and 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 the end. But I can't page backwards until the last page and even then I can't page back until the first page neither.

What am I missing please?

UPDATE:

Changed code with Faisal's suggestions. It works better I must admit. But still the paging doesn't work correctly:

I have 7 entries. PAGE_SIZE in config is 3. Hence we get three pages:

When clicking on Next I get 7,6,5 -> 4,3,2 -> 1 Perfect. Now when clicking on previous: 1 -> 3,4,5 (?) -> 5,6,7 (?)

Thanks for your 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 now its nearly working with reverse().

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

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

解决方案

So what I do here is use the current bookmark to for navigating for next or previous and removed the other query so it doesn't query twice for each request. (Edited the old description/answer was wrong when I tested it. This one works on my 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 %}

这篇关于如何使用ndb游标翻转到上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆