1

I hope you well. I’m developing a web using python and Django. I will love to paginate my dictionary “image_set_list” in order to see one element in one page, I mean, one element per page. The problem is that when I get the page, the result is None and pagination is not working.

This is my view.py

from django.shortcuts import render
from django.http import HttpResponse
import json
from pathlib import Path
import os
from django.core.paginator import Paginator


def detection_detail(request):
 
    # data planogram, bounding_boxes
    with open('detection/data.json', 'r') as json_file:
        data = json.load(json_file)
 
    # data image planogram
    image_name_location = data['image']
    image_planogram = os.path.basename(image_name_location)
 
    # data product images
    with open('detection/data_product_images.json', 'r') as json_file:
        data_product_images = json.load(json_file)
    
 
    data_product_image = {x["id"]: x for x in data_product_images}
    
 
    images_reference = []
    list_images = []
 
    for product in data['bounding_boxes']:
        product_id = product['class']
        
        product_ids = product['class_list']
        image_p = data_product_image[product_id]["image_file"]
        image_p_list = [data_product_image[id]["image_file"]
                        for id in product_ids]
        
        images_reference.append(image_p)
        list_images.append(image_p_list)
        
 
    image_set_list = {images_reference: list_images for images_reference,
                      list_images in zip(images_reference, list_images)}
    
    #PAGINATION!!
    paginator = Paginator(image_set_list , 1)
    page = request.GET.get('page')
    print('page', page)
    #result is -> None
    set = paginator.get_page(page)
 
    context = {
        'list_images': list_images, 'images_reference': images_reference,
        'image_planogram': image_planogram, 'image_set_list': image_set_list,
        'image_p_list': image_p_list, 'set': set}
 
    return render(request, "detection/detection_detail.html", context)

And this is my detection_detail.html (at the bottom you can see the pagination code.

                <div class="container">

                    {% for image_reference , list in image_set_list.items %}

                    <div class="container">

                        <!--Image Planogramme-->
                        <p> image planogram </p>
                        <a href="{% url 'detection_detail' %}">
                            <img class="img-responsive image-rezise planogram_image"
                                src="/static/images/{{image_planogram}}">
                        </a>
                        <!--End Image Planogramme-->

                        <!--Image Reference-->
                        <a href="{% url 'detection_detail' %}">
                            <p>image detection {{ image_reference }} </p>
                            <img class="img-responsive image-rezise main_image_detection"
                                src="/static/images/{{image_reference}}">
                        </a>
                        <!-- end Image Reference-->
                    </div>
                    <div class="container">
                        {% for i in list %}

                        <!-- list options Reference-->
                        <p>image option {{ i }}</p>

                        <a href="{% url 'detection_detail' %}">

                            <img class="img-responsive image-rezise set_image_detection" src="/static/images/{{i}}">
                        </a>
                        <!-- end list options Reference-->
                        {% endfor %}

                    </div>

                    {% endfor %}




                </div>

                <div class="container-fluid">

                    <div class="row">
                        <div class="pagination">
                            {% if set.has_previous %}
                            <a class="btn btn-primary m2" href="?page=1">&laquo; first</a>
                            <a class="btn btn-primary m-2" href="?page={{ set.previous_page_number }}">previous</a>
                            {% endif %}
                            {% if set.has_next %}
                            <a class="btn btn-primary m-2" href="?page={{ set.next_page_number }}">next</a>
                            <p class="current btn btn-primary m2"> Page {{ set.number }} of
                                {{ set.num_pages }}
                            </p>
                            <a class="btn btn-primary m-2" href="?page={{ set.num_pages }}">last &raquo;</a>
                            {% endif %}
    
    
                        </div>
                    </div>
    
            </div>

I see nothing related with pagination and I still see all the elements in the same page. I have being checking for a while but not idea what is going on. maybe is because it is a dictionary I can't do this?

I will really appreciate your help gyus! thanks in advance!

1 Answers1

1

It is printing None because there are no request param with a name of page on your browser URL. It might be probably example.com not this example.com/page=2

 ...

 #PAGINATION!!
    paginator = Paginator(image_set_list , 1)
    page = request.GET.get('page')
    print('page', page)
    #result is -> None
    set = paginator.get_page(page)
  
  ...

As the structure of the dictionary you are passing is not clear, what I would suggest you do is try Django Shell and run something like this (source)

>>> from django.core.paginator import Paginator
>>> d = {'a': [1,2], 'b': [3,4]}
>>> p = Paginator(d, 1)
>>> p1 = p.page(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type
>>> t = tuple(d.items())
>>> t
(('a', [1, 2]), ('b', [3, 4]))
>>> p = Paginator(t, 1)
>>> p.page(1).object_list
(('a', [1, 2]),)
>>> p.page(2).object_list
(('b', [3, 4]),)
  • Great! It worked with tuple, now the URL get the page!. Now my problem is that in my template I'm still seeing the hole items, I mean, it changes the page when I click "next" and "previous", but the template is not showing one element per page! What could be happening ? – Lady Geraldine Villamil Guerre Nov 14 '20 at 09:41