每当我按下指向下一页的链接(这是一个 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 %}
qq_花开花谢_0
相关分类