【发布时间】:2021-02-14 05:54:29
【问题描述】:
我有一个 Django 页面,其中有一个表格来显示我的模型中的数据。该表具有分页和多个过滤器。我已经设法让分页和过滤器在一定程度上协同工作,但是 URL 都搞砸了。看起来它正在将新的查询字符串附加到 URL 的末尾。
我有两个主要问题:
- 如果我使用单个过滤器,它会在所有过滤器的 URL 中添加所有空查询字符串。
- 如果我已过滤表中的数据并尝试更改页面,它会将页面查询字符串附加到末尾而不是替换它。
问题 1 的屏幕截图:
问题 2 的屏幕截图:
filters.py:
class SetFilter(django_filters.FilterSet):
name = CharFilter(field_name='name', lookup_expr='icontains', label='', )
type = AllValuesFilter(field_name='type', choices=Set.objects.values_list('type', flat=True).distinct().order_by('type'), label='', empty_label="")
nonfoil = AllValuesFilter(field_name='is_non_foil_only', choices=Set.objects.values_list('is_non_foil_only', flat=True).distinct().order_by('is_non_foil_only'), label='', empty_label="")
foil = AllValuesFilter(field_name='is_foil_only', choices=Set.objects.values_list('is_foil_only', flat=True).distinct().order_by('is_foil_only'), label='', empty_label="")
digital = AllValuesFilter(field_name='is_online_only', choices=Set.objects.values_list('is_online_only', flat=True).distinct().order_by('is_online_only'), label='', empty_label="")
foreign = AllValuesFilter(field_name='is_foreign_only', choices=Set.objects.values_list('is_foreign_only', flat=True).distinct().order_by('is_foreign_only'), label='', empty_label="")
class Meta:
model = Set
fields = ''
views.py
def sets_page(request):
set_list = Set.objects.order_by('-id')
last_modified = set_list[0].last_modified
set_filter = SetFilter(request.GET, queryset=set_list)
set_list = set_filter.qs
paginator = Paginator(set_list, 22)
is_paginated = True if paginator.num_pages > 1 else False
page = request.GET.get('page') or 1
try:
current_page = paginator.page(page)
except InvalidPage as e:
raise Http404(str(e))
context = {
'last_modified': last_modified,
'is_paginated': is_paginated,
'current_page': current_page,
'set_filter': set_filter
}
return render(request, "main/sets.html", context)
分页.html
{% if current_page.has_next %}
{% if 'name' in request.get_full_path %}
<a class="page-link previous-next" href="{{ request.get_full_path }}&page={{ current_page.next_page_number }}">»</a>
{% else %}
<a class="page-link previous-next" href="?page={{ current_page.next_page_number }}">»</a>
{% endif %}
{% else %}
<a class="page-link previous-next" href="">»</a>
{% endif %}
【问题讨论】:
标签: python python-3.x django django-filter django-pagination