Ошибка разбиения на страницы в Django: объект типа 'Custom Filter' не имеет len () - PullRequest
0 голосов
/ 07 апреля 2020

У меня есть несколько проблем с Django нумерацией страниц. Я продолжаю получать объект типа 'Custom Filter' не имеет ошибки len () `. Я не знаю, почему я продолжаю получать эту ошибку. Хотя у меня есть предчувствие, что ошибка могла быть вызвана моим использованием filter.qs. Кто-нибудь с предложением о том, как это можно решить?

views.py

def crm(request):
    #filtering the customer queryset
    customers_list = ProspectiveCustomer.objects.all()
    customers = CustomerFilter(request.GET, queryset=customers_list)
    has_filter = any(field in request.GET for field in set(customers.get_fields()))

    #pagination
    paginator = Paginator(customers_list, 10)  # Show 10 customers per page.

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    try:
        customers_list = paginator.page(page_number)
    except PageNotAnInteger:
        customers_list = paginator.page(1)
    except EmptyPage:
        customers_list = paginator.page(paginator.num_pages)
    return render(request, 'crm/crm.html', {
        ...
        'page_obj': page_obj,
        'filter': customers,
        'has_filter': has_filter,
    })

template

<!--Filter customers-->
<div class="container search-container">
    <form method="get">
        <div class="form-row">
            <div class="form-group col-md-4">
                {{ filter.form.customer_name|as_crispy_field}}
            </div>

            <div class="form-group col-md-4 filter-col">
                {{ filter.form.email|as_crispy_field}}
            </div>

            <div class="form-group col-md-4 filter-col">
                {{ filter.form.city|as_crispy_field}}
            </div>
        </div>
        <button class="btn btn-warning" type="submit"><i class="fa fa-search"></i> Search</button>
        {% if has_filter %}
        <a class="btn btn-secondary" href="{% url 'crm' %}" type="submit">
            <i class="fas fa-times"></i> Clear filter
        </a>
        {% endif %}
    </form>
</div>

<div class="container customer-container">
    <h5 class="page-header">Prospective Customers</h5>
    <table class="table table-striped table-hover" id="prospective-customer-table">
        <thead class="thead-dark">
        <tr>
            <th>#</th>
            <th>Customer Name</th>
            <th>Phone</th>
            <th>Email</th>
            <th>City</th>
            <th>Contacted?</th>
            <th>Change Status</th>
        </tr>
        </thead>
        <tbody>
        {% include 'crm/partial_customer_list.html' %}

        </tbody>
    </table>
    <ul class="pagination crm_pagination">
        {% if page_obj.has_previous %}
        <li><a href="?page={{ page_obj.previous_page_number }}"><i aria-hidden="true"
                                                                   class="fa fa-chevron-left"></i></a></li>
        {% else %}
        <li class="disabled"><span><i aria-hidden="true" class="fa fa-chevron-left"></i></span></li>
        {% endif %}

        {% if page_obj.number|add:'-4' > 1 %}
        <li><a href="?page={{ page_obj.number|add:'-5' }}">&hellip;</a></li>
        {% endif %}

        {% for i in page_obj.paginator.page_range %}
        {% if page_obj.number == i %}
        <li class="active bg-dark text-white"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
        {% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
        {% endif %}
        {% endfor %}

        {% if page_obj.paginator.num_pages > page_obj.number|add:'4' %}
        <li><a href="?page={{ page_obj.number|add:'5' }}">&hellip;</a></li>
        {% endif %}

        {% if page_obj.has_next %}
        <li><a href="?page={{ page_obj.next_page_number }}"><i aria-hidden="true" class="fa fa-chevron-right"></i></a>
        </li>
        {% else %}
        <li class="disabled"><span><i aria-hidden="true" class="fa fa-chevron-right"></i></span></li>
        {% endif %}

    </ul>
...