【问题标题】:Django pagination python dictionaryDjango分页python字典
【发布时间】:2020-11-14 09:00:43
【问题描述】:

希望你一切都好。 我正在使用 python 和 Django 开发一个网络。我很乐意对我的字典 “image_set_list” 进行分页,以便在一页中看到一个元素,我的意思是,每页一个元素。 问题是当我得到页面时,结果是None 并且分页不起作用。

这是我的观点.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)

这是我的 detection_detail.html(在底部你可以看到分页代码。

                <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>

我没有看到任何与分页相关的内容,但我仍然可以看到同一页面中的所有元素。我已经检查了一段时间,但不知道发生了什么。也许是因为它是一本字典我不能这样做?

我将非常感谢您的帮助 gyus!提前致谢!

【问题讨论】:

    标签: python django dictionary pagination


    【解决方案1】:

    它正在打印 None 因为在您的浏览器 URL 上没有带有页面名称的请求参数。可能是 example.com 而不是这个 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)
      
      ...
    

    由于您传递的字典的结构不清楚,我建议您尝试使用 Django Shell 并运行类似的东西 (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]),)
    

    【讨论】:

    • 太棒了!它与元组一起使用,现在 URL 获取页面!现在我的问题是,在我的模板中我仍然看到孔项,我的意思是,当我单击“下一步”和“上一个”时,它会更改页面,但模板没有每页显示一个元素!会发生什么?
    猜你喜欢
    • 2011-10-24
    • 2018-10-20
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2018-08-06
    • 2020-01-02
    • 1970-01-01
    相关资源
    最近更新 更多