Django(尝试将分页与过滤器配合使用)

时间:2018-11-27 12:52:33

标签: django

我正在尝试使用过滤器向我的查询集添加分页,过滤器似乎可以工作,但分页不起作用。有人可以让我知道我需要进行哪些更改才能使分页生效。 当我们进入第2页时,我们得到了整个查询结果,而不是过滤器    Django Filer +分页
下面是代码:

def index(request):
    user_list_all = MasterGidrDataDict.objects.all()

    user_filter = UserFilter(request.GET, queryset=user_list_all)
    user_list = user_filter.qs
    page = request.GET.get('page', 1)

    paginator = Paginator(user_list, 50)
    try:
        users = paginator.page(page)
    except PageNotAnInteger:
        users = paginator.page(1)
    except EmptyPage:
        users = paginator.page(paginator.num_pages)
    args = {'paginator': paginator, 'filter': user_filter, 'users': users}
    return render(request, 'app1/index.html', args)

index.html

<html>
        <head>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
        </head>

        {% block content %}
          <form method="get">
            {{ filter.form.as_p }}
            <button type="submit">Search</button>
          </form>


        <div class = "tabl" >
        <table class = 'table table-bordered'>
        <thead>
        <tr>
         <th style="color:black;"> id </th>
         <th style="color:black;"> vendor_name </th>
         <th style="color:black;"> market_name</th>
         <th style="color:black;"> grup </th>
         <th style="color:black;"> vrbl </th>
         <th style="color:black;"> code </th>
         <th style="color:black;"> output </th>
         <th style="color:black;"> active_flag </th>
         <th style="color:black;"> load_date <th>
        </tr>
        </thead>
        <tbody>
        {% for user in users %}
        <tr>
        <td>{{ user.id|upper }}</td>
        <td>{{user.vendor_name}}</td>
        <td>{{user.market_name}}</td>
        <td>{{user.grup}}</td>
        <td>{{user.vrbl}}</td>
        <td>{{user.code}}</td>
        <td>{{user.output}}</td>
        <td>{{user.active_flag}}</td>
        <td>{{user.load_date}}</td>
        </tr>
        {% endfor %}
        </tbody>
        </table>
        </div>

        <div class="pagination">
            <span class="step-links">
                {% if users.has_previous %}
                    <a href="?page=1">&laquo; first</a>
                    <a href="?page={{ users.previous_page_number }}">previous</a>

                {% endif %}

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

                {% if users.has_next %}
                    <a href="?page={{ users.next_page_number }}">next</a>
                    <a href="?page={{ users.paginator.num_pages }}">last &raquo;</a>

                {% endif %}
            </span>
        </div>

        {% endblock %}

</html>

2 个答案:

答案 0 :(得分:0)

这是分页和过滤如何协同工作的一个常见问题,有些情况需要您自己解决。

Caktus Group的

This article详细描述了该问题,但基本上,您需要在模板中建立链接以保存以及页码。这是因为您正在使用GET方法来提交过滤表单,这意味着数据将作为查询参数(例如页码)传递。

在已过滤页面上,分页链接不包括当前过滤器。

答案 1 :(得分:0)

您的看法对我来说很好。

对于您的模板,在过滤器部分{% for user in users %}中,我的操作有所不同,我做了类似{% for user in users.object_list %}的操作。

在分页部分,我添加了一个模板标签,以使用过滤器呈现url。如果您不担心附加到url,则可以使用{{ request.get_full_path }},例如<a href="{{ request.get_full_path }}&page={{ dataqs.next_page_number }}">next</a>

来在没有模板标记的情况下对url进行编码,这是一种更简单的方法。

在下面的视图中查看我的视图和模板文件。

search_view.py

from django.shortcuts import render
from app.models.filters_model import ApiStatusFilter
from app.models.api_status import ApiStatus
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from datetime import datetime, timedelta

def status(request):
    all_entries_ordered = ApiStatus.objects.values().order_by('-created_at')[:200]

    for dictionarys in all_entries_ordered:
        dictionarys

    apistatus_list = ApiStatus.objects.values().order_by('-created_at')
    apistatus_filter = ApiStatusFilter(request.GET, queryset=apistatus_list)

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

    return render(request, 'status_page_template.html', {'dictionarys': dictionarys, 'apistatus_filter': apistatus_filter, 'dataqs': dataqs, 'allobjects': apistatus_list})

status_template.html

{% load static %}
{% load my_templatetags %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="{% static 'css/table_styling.css' %}">
        <meta charset="UTF-8">
        <title>TEST</title>
    </head>

    <body>
         <table>
            <thead>
                <tr>
                    {% for keys in dictionarys.keys %} 
                        <th>{{ keys }}</th>
                    {% endfor %}
                </tr>
            </thead>
                <form method="get">
                    {{ apistatus_filter.form.as_p }}
                    <button type="submit">Search</button>
                        {% for user in dataqs.object_list %}
                        <tr>
                            <td>{{ user.id }}</td>
                            <td>{{ user.date_time }}</td>
                            <td>{{ user.log }}</td>
                        </tr>
                        {% endfor %}
                </form>
            </tbody>
        </table>

        <div class="pagination">
            <span>
                {% if dataqs.has_previous %}
                    <a href="?{% query_transform request page=1 %}">&laquo; first</a>
                    <a href="?{% query_transform request page=dataqs.previous_page_number %}">previous</a>
                {% endif %}

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

                {% if dataqs.has_next %}
                    <a href="?{% query_transform request page=dataqs.next_page_number %}">next</a>
                    <a href="?{% query_transform request page=dataqs.paginator.num_pages %}">last &raquo;</a>
                {% endif %}
            </span>
        </div> 
    </body>
</html>

my_templatetags.py

from django import template

register = template.Library()

@register.simple_tag
def query_transform(request, **kwargs):
    updated = request.GET.copy()
    for k, v in kwargs.items():
        if v is not None:
            updated[k] = v
        else:
            updated.pop(k, 0)

    return updated.urlencode()