Search code examples
djangodjango-templatespaginationdjango-pagination

Django template large data pagination link issue


I am using the django paginator in my template. Its working ok with less data, but not working good when there's large numbers of pages.

Pagination links code is given bellow:

<div class="paginationWrapper mt-3">
        {% if match_list.paginator.num_pages > 1 %}
        <ul class="pagination">
            {% if match_list.has_previous %}
            <li>
                <a href="?p={{ match_list.previous_page_number }}
                    {% if request.GET.search %}&search={{ request.GET.search }}
                    {% endif %}" class="page-link"><i class="fas fa-angle-double-left"></i></a>
            </li>
            {% endif %}
            {% for j in match_list.paginator.page_range %}
            {% if match_list.number == j %}
            <li class="active"><a class="page-link" href="#">{{ j }}</a></li>
            {% else %}
            <li><a href="?p={{ j }}{% if request.GET.search %}&search={{ request.GET.search }}
                    {% endif %}" class="page-link">{{ j }}</a></li>

            {% endif %}
            {% endfor %}

            {% if match_list.has_next %}
            <li>
                <a href="?p={{ match_list.next_page_number }}  
                {% if request.GET.search %}&search={{ request.GET.search }}{% endif %}            
                    " class="page-link"><i class="fas fa-angle-double-right"></i></a>
            </li>
            {% endif %}
        </ul>
        {% endif %}
    </div>

After this i am gettin the links in my template like :

enter image description here

what i actually want is i want to display only 10 links after that ... to show more can anyone please help me relatred this i am stuck here


Solution

  • https://docs.djangoproject.com/en/3.1/topics/pagination/#paginating-a-listview

    try this

    {% load core %}   // core here is template tag
    {% if match_list.paginator.num_pages > 1 %}
            <ul class="pagination justify-content-center">
                {% if match_list.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=1 %}">
                            First
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#">
                            First
                        </a>
                    </li>
                {% endif %}
                {% if match_list.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=match_list.previous_page_number %}">
                            &laquo;
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#">
                            &laquo;
                        </a>
                    </li>
                {% endif %}
                {% for i in match_list.paginator.page_range %}
                    {% if match_list.number == i %}
                        <li class="page-item active">
                            <a class="page-link" href="#">{{ i }} <span class="sr-only">(current)</span>
                            </a>
                        </li>
                    {% elif match_list.number > i|add:"-5" and match_list.number < i|add:"+5"%}
                        <li class="page-item"><a class="page-link" href="?{% param_replace page=i %}">{{ i }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if match_list.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=match_list.next_page_number %}">
                            &raquo;
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#">
                            &raquo;
                        </a>
                    </li>
                {% endif %}
                {% if match_list.paginator.num_pages != match_list.number %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=match_list.paginator.num_pages %}">
                            Last
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><a class="page-link" href="#">Last</a></li>
                {% endif %}
            </ul>
    {% endif %}
    

    custom template tag

    @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()
    

    output

    enter image description here