有没有办法将分页与django过滤器一起使用?

每当我按下指向下一页的链接(这是一个 get 请求)时,都会绕过筛选器,并且我将获得整个列表的相应页面(没有筛选器)。例如,如果我在第 1 页上查看已过滤的列表,然后单击“下一步”,则整个未筛选列表的第 2 页登陆。在 models.py,


class Person(models.Model):

    name = models.CharField(max_length=50, unique=True)

    gender = models.CharField(max_length=7, choices=GENDER_CHOICES)

    category = models.CharField(max_length=20, choices=get_all_category_choices())

在 filters.py,


import django_filters


from .models import Person


class PersonFilter(django_filters.FilterSet):


    class Meta:

        model = Person

        fields = [

            'name',

            'gender',

            'category',

        ]

在 views.py,


def show_all_persons_page(request):

    context = {}


    filtered_person_list = PersonFilter(

        request.GET,

        queryset=Person.objects.all()

    )


    context['filtered_person_list'] = filtered_person_list


    paginated_filtered_person_list = Paginator(filtered_person_list.qs, 3)

    page_number = request.GET.get('page')

    person_page_obj = paginated_filtered_person_list.get_page(page_number)


    context['person_page_obj'] = person_page_obj


    return render(request, 'app1/show_all_persons_page.html', context)

在 app1/show_all_persons_page.html,


<form method="get">

    {{ filtered_person_list.form.as_p }}

    <button type="submit">Search</button>

</form>


{% for person in person_page_obj %}

    <img src="{{ person.picture.url }}" width="240">

    <h2>

        {{person.name}}

        <br>

        {{person.gender}}

        <br>

        {{person.category}}

        <br>

    </h2>

    <br>

{% endfor %}


蛊毒传说
浏览 98回答 1
1回答

qq_花开花谢_0

解决。从&nbsp;https://simpleisbetterthancomplex.com/snippet/2016/08/22/dealing-with-querystring-parameters.html&nbsp;实现的概念。为了保留当前的 url 参数,我们必须定义一个自定义模板标记。在 app1 下创建一个名为模板标记的新目录。在该目录中创建 一个。创建另一个.py文件,比如app1_extras。在app1_extras中,定义自定义模板标记,如下所示。app1/模板标签/app1_extras.py,__init__.pyfrom django import templateregister = template.Library()@register.simple_tagdef relative_url(value, field_name, urlencode=None):&nbsp; &nbsp; url = '?{}={}'.format(field_name, value)&nbsp; &nbsp; if urlencode:&nbsp; &nbsp; &nbsp; &nbsp; querystring = urlencode.split('&')&nbsp; &nbsp; &nbsp; &nbsp; filtered_querystring = filter(lambda p: p.split('=')[0] != field_name, querystring)&nbsp; &nbsp; &nbsp; &nbsp; encoded_querystring = '&'.join(filtered_querystring)&nbsp; &nbsp; &nbsp; &nbsp; url = '{}&{}'.format(url, encoded_querystring)&nbsp; &nbsp; return url这将获取我们当前的 URL,并确保参数与页码一起就位。field_name是字符串“page”,值是从模板传递的页码。网址代码将我们当前的网址表示为字符串。在 app1/show_all_persons.html,{% load app1_extras %}<form method="get">&nbsp; &nbsp; {{ filtered_person_list.form.as_p }}&nbsp; &nbsp; <button type="submit">Search</button></form>{% for person in person_page_obj %}&nbsp; &nbsp; <img src="{{ person.picture.url }}" width="240">&nbsp; &nbsp; <h2>&nbsp; &nbsp; &nbsp; &nbsp; {{person.name}}&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; {{person.gender}}&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; {{person.category}}&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <br>{% endfor %}<div class="pagination">&nbsp; &nbsp; <span class="step-links">&nbsp; &nbsp; &nbsp; &nbsp; {% if person_page_obj.has_previous %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{% relative_url 1 'page' request.GET.urlencode %}">&laquo; first</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{% relative_url person_page_obj.previous_page_number 'page' request.GET.urlencode %}">previous</a>&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; &nbsp; &nbsp; <span class="current">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Page {{ person_page_obj.number }} of {{ person_page_obj.paginator.num_pages }}.&nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; &nbsp; {% if person_page_obj.has_next %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{% relative_url person_page_obj.next_page_number 'page' request.GET.urlencode %}">next</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{% relative_url person_page_obj.paginator.num_pages 'page' request.GET.urlencode %}">last &raquo;</a>&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; </span></div>{% 加载 app1_extras %} 将加载我们新的自定义模板标记。relative_url函数现在被传递给我们页面链接的hrefs。参数按请求的顺序传递。GET.网址代码将包含我们当前的网址,并将传递给网址代码参数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python