【问题标题】:Filtering and pagination in Django 2xDjango 2x 中的过滤和分页
【发布时间】:2019-05-18 16:31:37
【问题描述】:

看起来 django-filter-mixin 在 Django 2x 中仍然存在问题。所以我试图用“老式的方式”来做。以下从正常工作的过滤条件开始,分页代码已包含但无法正常工作,我无法确定这是视图还是模板(均包含)的问题

views.py

def allcontainer(request):
    allcontainer = Container.objects.all()
    container_list = Container.objects.all()
    user_list = User.objects.all()

    type = request.GET.get('type')
    name = request.GET.get('name')
    rack = request.GET.get('rack')
    shelf = request.GET.get('shelf')

    if (
    type =='' or type is None and
    name =='' or name is None and
    rack =='' or rack is None and
    shelf =='' or shelf is None
    ):
        allcontainer = allcontainer

    if type !='' and type is not None:
        allcontainer = allcontainer.filter(container_type__iexact=type)
    if name !='' and name is not None:
        allcontainer = allcontainer.filter(container_name__iexact=name)
    if rack !='' and rack is not None:
        allcontainer = allcontainer.filter(location_id__location_name__iexact=rack)
    if shelf !='' and shelf is not None:
        allcontainer = allcontainer.filter(location_id__location_sub_name__iexact=shelf)

    qs = allcontainer
    paginator = Paginator(qs, 25)
    page = request.GET.get('page')
    try:
        pub = paginator.page(page)
    except PageNotAnInteger:
        pub = paginator.page(1)
    except EmptyPage:
       pub = paginator.page(paginator.num_pages)
    # url_filter = PublicationFilter(request.GET, queryset=qs)

    context = {
    'container':allcontainer,
    'type': type,
    'pub':pub,
    # 'url_filter':url_filter
    # name
    # rack
    # shelf
    }

    return render(request, 'container/allcontainer.html', context)

模板

...

  {% if pub.has_other_pages %}
  <p>a</p>
  {% if pub.has_previous %}
  <p>b</p>
  <a href="?page={{ pub.previous_page_number }}">previous</a>
  <!-- </li> -->
  {% else %}
  <li class="disabled"><span>???</span></li>
  {% endif %}
  {% for i in pub.paginator.page_range %}
  {% if pub.number == i %}
  <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
  {% else %}
  <li><a href="?page={{ i }}">{{ i }}</a></li>
  {% endif %}
  {% endfor %}
  {% if pub.has_next %}
  <li><a href="?page={{ pub.next_page_number }}">next</a></li>
  {% else %}
  <li class="disabled"><span>&raquo;</span></li>
  {% endif %}

  {% endif %}
</section>

...

【问题讨论】:

    标签: django pagination filtering


    【解决方案1】:

    在你的代码中替换

    pub = paginator.page(page)

    pub = paginator.get_page(page)

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 2019-08-14
      • 2018-05-11
      • 1970-01-01
      • 2021-03-29
      • 2021-10-01
      • 2018-07-10
      • 2019-08-21
      • 2023-03-06
      相关资源
      最近更新 更多