【发布时间】: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">« 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 »</a>
{% endif %}
</div>
</div>
</div>
我没有看到任何与分页相关的内容,但我仍然可以看到同一页面中的所有元素。我已经检查了一段时间,但不知道发生了什么。也许是因为它是一本字典我不能这样做?
我将非常感谢您的帮助 gyus!提前致谢!
【问题讨论】:
标签: python django dictionary pagination