【问题标题】:Django Pagination & Filters appending to url rather than replacingDjango 分页和过滤器附加到 url 而不是替换
【发布时间】:2021-02-14 05:54:29
【问题描述】:

我有一个 Django 页面,其中有一个表格来显示我的模型中的数据。该表具有分页和多个过滤器。我已经设法让分页和过滤器在一定程度上协同工作,但是 URL 都搞砸了。看起来它正在将新的查询字符串附加到 URL 的末尾。

我有两个主要问题:

  1. 如果我使用单个过滤器,它会在所有过滤器的 URL 中添加所有空查询字符串。
  2. 如果我已过滤表中的数据并尝试更改页面,它会将页面查询字符串附加到末尾而不是替换它。

问题 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 }}">&#187;</a>
    {% else %}
       <a class="page-link previous-next" href="?page={{ current_page.next_page_number }}">&#187;</a>
    {% endif %}
{% else %}
    <a class="page-link previous-next" href="">&#187;</a>
{% endif %}

【问题讨论】:

    标签: python python-3.x django django-filter django-pagination


    【解决方案1】:

    我一直在使用我不久前在这里找到的模板标签解决方案,本可以引用它,但我只会发布 sn-p:

    1. 如果您不知道如何创建模板标签。 Read here。这很容易,而且任务也不长。

    2. 在您的过滤器/标签文件中添加以下代码:

    @register.simple_tag(takes_context=True)
    def param_replace(context, **kwargs):
        d =context['request'].GET.copy()
        for k,v in kwargs.items():
            d[k] = v
        for k in [k for k,v in d.items() if not v]:
            del d[k]
        return d.urlencode()
    

    在您的模板{% load "file_name_here" ... %}中加载您的自定义过滤器

    然后在您的分页链接上:

    <a class="page-link" href="?{% param_replace page=current_page.next_page_number %}"> Next </a>
    
    

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 2019-08-17
      • 2019-07-03
      • 1970-01-01
      • 2011-04-22
      • 2021-07-03
      • 1970-01-01
      • 2019-08-14
      • 2016-05-29
      相关资源
      最近更新 更多