0

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

Pankhuri
  • 25
  • 7

1 Answers1

1

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

rahul.m
  • 5,572
  • 3
  • 23
  • 50
  • not working error : Invalid block tag on line 77: 'param_replace', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? and one more thing cant you edit my design ?? cos i dont want to compromise with design – Pankhuri Mar 19 '21 at 06:41
  • This is not working my bad .can you please update my html code with your logic ?? because i need that design only and i am passing the querystring as p in url. please update my html with your conditions – Pankhuri Mar 19 '21 at 06:55
  • @Pankhuri you can try this https://stackoverflow.com/a/46329564/5600452 – rahul.m Mar 19 '21 at 07:02
  • same i tried before but that is not working with my design help me out @c.grey i am really stuck here – Pankhuri Mar 19 '21 at 07:17
  • Thanks @c.grey it working now thanks a lot – Pankhuri Mar 19 '21 at 07:28