django分页查询的下一个和上一个链接 [英] Next and Before Links for a django paginated query

查看:95
本文介绍了django分页查询的下一个和上一个链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Django创建搜索表单.

I'm trying to make a search form for Django.

它是典型的搜索表单,然后返回匹配表.我希望对返回的表格进行分页.

Its a typical search form and then returns a table of matches. I wish to paginate the tables returned.

问题出在上一个"和下一个"按钮上. 返回查询的链接转到/records/search/?query=a(搜索示例为a) 该页面输出表格及其上一个和下一个链接.但是,链接重定向到/records/search/?page=2,并且页面显示空白表.

The problem lies in the Previous and Next buttons. The links for the return query goes to /records/search/?query=a (search sample is a) The page outputs the table and its previous and next links. However the links redirect to /records/search/?page=2 and the page displays a blank table.

上一个/下一个我应该通过哪些链接的帮助?

Any help on which links I should pass for Prev/Next?

search.html :

{% extends 'blank.html' %}

{% block content %}

    <div class="row">
    <form id="search-form" method="get" action=".">
      {{ form.as_p }}
      <input type="submit" value="Search" />
    </form>
    </div>
    <br><br>

//display table code//

{% if is_paginated %}
<div class="pagination">
    <span class="step-links">
        {% if agent_list.has_previous %}
            <a href="?page={{ agent_list.previous_page_number }}{% for key,value in request.GET.items %}{% ifnotequal key 'page' %}&{{ key }}={{ value }}{% endifnotequal %}{% endfor %}">forrige</a>
        {% endif %}

        <span class="current">
            Page {{ agent_list.number }} of {{ agent_list.paginator.num_pages }}.
        </span>

        {% if agent_list.has_next %}
            <a href="?page={{ agent_list.next_page_number }}">Next</a>
        {% endif %}
    </span>
</div>
{% endif %}

{% endblock %}

和搜索视图:

def search_page(request):
    form = SearchForm()
    agents = []
    show_results=False

    if request.GET.has_key('query'):
        show_results=True
        query=request.GET['query'].strip()
        if query:
            form=SearchForm({'query': query})
            agents = \
                Agent.objects.filter(Q(name__icontains=query))


    paginator = Paginator(agents, 10)
    page = request.GET.get('page')
    try:
        agents = paginator.page(page)
    except PageNotAnInteger:
        agents = paginator.page(1)
    except EmptyPage:
        agents = paginator.page(paginator.num_pages)


    variables = RequestContext(request, 
        {   'form': form,
            'agent_list': agents,
            'show_results': show_results,
            'is_paginated': True,
        }
    )

    return render_to_response('search.html', variables)

我见过类似的问题,但我听不懂/使它们起作用.有什么帮助吗?


快速修复(还没有真正了解缺点)

I've seen the similar questions but I can't understand/make them work. Any help?


For a quickfix (haven't really looked at the cons)

我在视图中添加了一个变量:

I added a variable in my view:

variables = RequestContext(request, 
    {   'form': form,
        'agent_list': agents,
        'show_results': show_results,
        'is_paginated': True,
        **'query': query,**
    }
)

其中不带引号的查询是收到的query变量.

Where query without the quotes is the recieved query variable.

然后只需将URL更改为:

Then simply change the URL to:

<a href="**?query={{query}}**&page={{ agent_list.previous_page_number }}">Previous</a>

如果您想更好地回答问题,请在当前打开的URL上添加一个URL或在其后面附加一个URL.

If you have a better way of answering the question, please do or appending a URL to your currently opened URL.

推荐答案

我建议将解决方案放入模板标记中,如下所示:

I would recommend putting the solution in a template tag like so:

myapp/templatetags/mytemplatetags.py:

myapp/templatetags/mytemplatetags.py:

from django import template
register = template.Library()

@register.simple_tag
def url_replace(request, field, value):
    d = request.GET.copy()
    d[field] = value
    return d.urlencode()

@register.simple_tag
def url_delete(request, field):
    d = request.GET.copy()
    del d[field]
    return d.urlencode()

然后从模板执行以下操作:

Then from templates do:

{% load mytemplatetags %}
...
<a href="?{% url_replace request 'page' agent_list.previous_page_number %}">previous</a>

这篇关于django分页查询的下一个和上一个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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