Django 2x中的过滤和分页

时间:2019-05-17 23:18:59

标签: django pagination filtering

看起来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>

...

1 个答案:

答案 0 :(得分:1)

在您的代码中替换

pub = paginator.page(page)

pub = paginator.get_page(page)