如何用ndb光标翻到上一页?

问题描述 投票:0回答:2

我无法在ndb分页中访问“上一页”。

我已经检查了文档以及类似的问题,但没有成功。

 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 %}

我可以正确翻页直到最后。 但我无法向后翻页直到最后一页,即使这样我也无法向后翻页直到第一页。

请问我缺少什么?

更新:

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

我有 7 个条目。配置中的 PAGE_SIZE 为 3。因此我们得到三页:

单击“下一步”时,我得到 7,6,5 -> 4,3,2 -> 1 Perfect。 现在,当单击上一个时:1 -> 3,4,5 (?) -> 5,6,7 (?)

感谢您的帮助

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)

更新2:

现在看来它几乎可以与reverse()一起使用了。

7,6,5 -> 下一个 -> 4,3,2 -> 下一个 -> 1

1 -> 上一个 -> 2,3,4 -> 5,6,7 (顺序不再是最新日期在前)

python google-app-engine pagination app-engine-ndb database-cursor
2个回答
9
投票

因此,我在这里所做的是使用当前书签导航下一个或上一个,并删除其他查询,这样它就不会为每个请求查询两次。 (编辑旧的描述/答案是错误的,当我测试它时。这个在我的本地主机上工作)。

尝试:

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 %}

0
投票

在这里您有一个完整的工作解决方案。你的代码有问题。

他们的关键是在倒退时反转结果。这很棘手。

在这里你有:

def return_query_page(query_class, size=10, bookmark=None, is_prev=None, equality_filters=None, orders=None):
    """
    Generate a paginated result on any class
    Param query_class: The ndb model class to query
    Param size: The size of the results
    Param bokkmark: The urlsafe cursor of the previous queries. First time will be None
    Param is_prev: If your requesting for a next result or the previous ones
    Param equal_filters: a dictionary of {'property': value} to apply equality filters only
    Param orders: a dictionary of {'property': '-' or ''} to order the results like .order(cls.property)
    Return: a tuple (list of results, Previous cursor bookmark, Next cursor bookmark)
    """
    if bookmark:
        cursor = ndb.Cursor(urlsafe=bookmark)
    else:
        is_prev = None
        cursor = None

    q = query_class.query()
    try:
        for prop, value in equality_filters.iteritems():
            q = q.filter(getattr(query_class, prop) == value)

        q_forward = q.filter()
        q_reverse = q.filter()

        for prop, value in orders.iteritems():
            if value == '-':
                q_forward = q_forward.order(-getattr(query_class, prop))
                q_reverse = q_reverse.order(getattr(query_class, prop))
            else:
                q_forward = q_forward.order(getattr(query_class, prop))
                q_reverse = q_reverse.order(-getattr(query_class, prop))
    except:
        return None, None, None
    if is_prev:
        qry = q_reverse
        new_cursor = cursor.reversed() if cursor else None
    else:
        qry = q_forward
        new_cursor = cursor if cursor else None

    results, new_cursor, more = qry.fetch_page(size, start_cursor=new_cursor)
    if more and new_cursor:
        more = True
    else:
        more = False

    if is_prev:
        prev_bookmark = new_cursor.reversed().urlsafe() if more else None
        next_bookmark = bookmark
        results.reverse()
    else:
        prev_bookmark = bookmark
        next_bookmark = new_cursor.urlsafe() if more else None

    return results, prev_bookmark, next_bookmark

这是 github 项目的链接:https://github.com/janscas/ndb-gae-pagination

© www.soinside.com 2019 - 2024. All rights reserved.