Search code examples
pythondjangodjango-viewsdjango-pagination

Failed lookup for key - implementing pagination with elided_page


I am attempting to implement pagination with elided_page based on this blog post. My goal is to have pagination with only a few pages being showed and increase as a new page is viewed as I will eventually have over a hundred pages.

view

def home(request):

page = request.GET.get('page', 1)
paginator = Paginator(Post.objects.all(), 2)
page_range = paginator.get_elided_page_range(number=page)

context = { 'page_range':page_range, 'page':page, 'paginator':paginator}

return render(request, 'blog/home.html', context)

template

<ul class="pagination justify-content-center flex-wrap mt-2 mb-4">
{% if page_obj.has_previous %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
{% else %}
    <li class="disabled page-item"><span class="page-link">&laquo;</span></li>
{% endif %}
{% for i in page_range|default_if_none:page_obj.paginator.get_elided_page_range %}
    {% if page_obj.number == i %}
        <li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span>
        </li>
    {% else %}
        {% if i == page_obj.paginator.ELLIPSIS %}
            <li class="page-item"><span class="page-link">{{ i }}</span></li>
        {% else %}
            <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
        {% endif %}
    {% endif %}
{% endfor %}
{% if page_obj.has_next %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
{% else %}
    <li class="disabled page-item"><span class="page-link">&raquo;</span></li>
{% endif %}

However when i run this i get the error

Failed lookup for key [page_obj] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'page_range': <generator object Paginator.get_elided_page_range at 0x000001FB70D20E40>, 'page': '3', 'paginator': <django.core.paginator.Paginator object at 0x000001FB70D7A490>}]

on this line

{% for i in page_range|default_if_none:page_obj.paginator.get_elided_page_range %}

if I edit that line and just have it as {% for i in page_range %} it sort of works but displays every page leading me to believe the error is with this part of the statement |default_if_none:page_obj.paginator.get_elided_page_range

enter image description here

I am not that great with the templates but in my research I came across this stack post. Where a user was experiencing a similar issue and say that this is just a bug in django that wont be fixed. Is this true? If not how would I remedy this issue or am I just going about this the wrong way.


Solution

  • It seems the actual page was not processed and is missing.

    Try to add this line:

    page_obj = paginator.get_page(page)
    

    And pass it to the context. So the full view would be:

    def home(request):
        page = request.GET.get('page', 1)
        paginator = Paginator(Post.objects.all(), 2)
        page_obj = paginator.get_page(page)
        page_range = paginator.get_elided_page_range(number=page)
    
        context = {'page_range': page_range, 'page': page, 'paginator': paginator, 'page_obj': page_obj}
    
        return render(request, 'blog/home.html', context)