在添加过滤器之前,分页没有问题。添加了一个基于django-filters的过滤器,现在只有第一页提供了正确的上下文,从第二页开始-将显示实例的完整列表。
我认为问题出在模板中的reques.GET中,以获取过滤后的值。事实证明,过滤器和分页都使用GET方法,当切换到分页的第二页时,触发了过滤器的if request.GET条件,并且由于过滤器不包含任何限制,因此元素的完整列表从所有页面输出。也许我错了,原因有所不同。帮助我了解如何进行分页和过滤。
转到分页的任何页面时,分页的输出都会消失。此行为对应于过滤时对象的显示。因为最初,我没有设置任务将分页添加到过滤器的结果中,而是仅将其应用于完整的未过滤列表。
templates.html
{% block content %}
<!-- Page Content -->
<div class="container">
<!-- Filter -->
<header class="custom-top">
<form method="get"> ### filters form
<div class="well">
<div class="row">
<div class="form-group col-sm-4 col-md-3 ">
<b>{{ filter.form.direction.label_tag }}</b>
<div class="clear"></div>
{% render_field filter.form.direction class="custom-multiple-select" %}
</div>
<div class="form-group col-sm-4 col-md-3">
<b>{{ filter.form.group.label_tag }}</b>
<div class="clear"></div>
{% render_field filter.form.group class='custom-select' %}
</div>
</div>
<div class="row">
<div class="form-group col-sm-4 col-md-3">
<input type="submit" value="Apply" class="btn btn-success filter-button">
<a href='{% url 'trainers:viewtrainers' trainer_city %}' class="btn btn-secondary filter-button float-lg-right">Clear</a>
</div>
</div>
</div>
</form>
</header>
{% if request.GET %} ### if filters apply
<div class="row text-center">
{% for trainer in filter.qs %}
{% if trainer.is_active %}
<div class="col-lg-3 col-md-6 mb-4">
<div class="card h-100">
{% if trainer.img %}
<div class="cardimg">
<img class="card-img-top" src="{{ MEDIA_URL }}{{ trainer.img.url }}">
</div>
{% else %}
<img class="card-img-top" src="http://placehold.it/700x400">
{% endif %}
<div class="card-body ">
<h4 class="card-title">
<a href="{% url 'trainers:viewperson' trainer.slug %}">{{ trainer.name }}</a>
</h4>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
<!-- Page Features -->
{% else %} ### if filter is not used
{% if trainers %}
<div class="row text-center">
{% for trainer in trainers %}
{% if trainer.is_active %}
<div class="col-lg-3 col-md-6 mb-4">
<div class="card h-100">
{% if trainer.img %}
<div class="cardimg">
<img class="card-img-top" src="{{ MEDIA_URL }}{{ trainer.img.url }}">
</div>
{% else %}
<img class="card-img-top" src="http://placehold.it/700x400">
{% endif %}
<div class="card-body ">
<h4 class="card-title">
<a href="{% url 'trainers:viewperson' trainer.slug %}">{{ trainer.name }}</a>
</h4>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
<!-- Pagination--> ### Paginations
{% if trainers.has_other_pages %}
<ul class="pagination justify-content-center" style="margin:20px 0">
{% if trainers.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ trainers.previous_page_number }}">«</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link" >«</span></li>
{% endif %}
{% for i in trainers.paginator.page_range %}
{% if trainers.number == i %}
<li class="page-item active"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if trainers.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ trainers.next_page_number }}">»</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link" >»</span></li>
{% endif %}
</ul>
{% endif %}
{% endif %}
</div>
<!-- /.container -->
{% endblock content %}