从分页和过滤器Django中重用旧的选项



view:

def subcategory(request, category_url):
category = get_object_or_404(Category, category_url=category_url)
subcategories = Subcategory.objects.filter(category=category)
products_list = Product.objects.filter(product_category=category)
subcatid = request.GET.getlist('subcategory')
print(subcatid)
if subcatid:
ids = [int(id) for id in subcatid]
subcategories1 = Subcategory.objects.filter(category=category, id__in=ids)
products_list = Product.objects.filter(product_category=category, product_subcategory__in=subcategories1)
else:
subcategories1 = None
products_list = Product.objects.filter(product_category=category)
page = request.GET.get('page', 1)
paginator = Paginator(products_list, 12)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
if request.user.is_authenticated:
wishlist = get_object_or_404(Wishlist, user=request.user.profile)
else:    
wishlist = None   
reqget = request.GET.copy()
reqget.pop('page', None)      
ctx = {'products':products,
'products_list':products_list,
'wishlist':wishlist,
'subcategories':subcategories,
'category':category,
'subcategories1':subcategories1,
'reqget': reqget,
}
return render(request, 'products/subcategory.html', ctx) 

模板:

{% extends "blog/base.html" %}
{% load static %}
{% load ratings %}
{% load humanize %}

{% block title %}ماهوت کالکشن | دسته بندی محصولات {% endblock title %}
{% block content %}
<main class="main">
<div class="page-header text-center">
<div class="container">
<h1 class="page-title">{{category.category_name}}<span>دسته بندی</span></h1>
</div><!-- End .container -->
</div><!-- End .page-header -->
<nav aria-label="breadcrumb" class="breadcrumb-nav mb-2">
<div class="container">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="">{{q}} فروشگاه</a></li>
<li class="breadcrumb-item"><a href="/products/">دسته بندی</a></li>
<li class="breadcrumb-item active" aria-current="page">{{category.category_name}}</li>
</ol>
</div><!-- End .container -->
</nav><!-- End .breadcrumb-nav -->
<div class="page-content">
<div class="container">
<div class="row">
<div class="col-lg-9">
<div class="toolbox">
<div class="toolbox-left">
<div class="toolbox-info">
محصولات این دسته : <span>{{products_list.all|length}}</span> محصول
</div><!-- End .toolbox-info -->
</div><!-- End .toolbox-left -->
<div class="toolbox-right">
<div class="toolbox-sort">
<label for="sortby">مرتب سازی براساس :</label>
<div class="select-custom">
<select name="sortby" id="sortby" class="form-control">
<option value="popularity" selected="selected">بیشترین خرید</option>
<option value="rating">محبوب ترین</option>
<option value="date">بر ارساس تاریخ</option>
<option value="">ارزان ترین</option>
<option value="">گران ترین</option>
</select>
</div>
</div><!-- End .toolbox-sort -->
</div><!-- End .toolbox-right -->
</div><!-- End .toolbox -->
<div class="products mb-3">
<div class="row justify-content-start">
{% for cat in products %}
<div class="col-6 col-md-4 col-lg-4 col-xl-3">
<div class="product product-7 text-center">
<figure class="product-media">
{% if cat.product_all_price == "0" %}
<span class="product-label label-out">ناموجود</span>
{% elif cat.discount_price %}
<span class="product-label label-new">فروش ویژه</span>
{% endif %} 
<a href="{{cat.product_url}}/">
<img src="{{cat.product_img1.url}}" alt="Product image" class="product-image">
</a>
<div class="product-action-vertical">
{% if cat in wishlist.items.all %}
<a href="/products/delete-from-wishlist/{{cat.id}}/" class="btn-product-icon btn-wishlist btn-expandable"><span>حذف</span></a>
{% else %}
<a href="/products/add-to-wishlist/{{cat.id}}/" class="btn-product-icon btn-wishlist btn-expandable"><span>افزودن به علاقه مندی ها</span></a>
{% endif %} 
</div><!-- End .product-action-vertical -->
<div class="product-action">
<a href="/products/category/{{cat.product_category.category_url}}/{{cat.product_subcategory.subcategory_url}}/{{cat.product_url}}" class="btn-product btn-cart"><span>مشاهده محصول</span></a>
</div><!-- End .product-action -->
</figure><!-- End .product-media -->
<div class="product-body">
<div class="product-cat">
<a href="/products/category/{{cat.product_category.category_url}}">
{% if not cat.product_subcategory.subcategory_name %}
{{cat.product_category.category_name}}
{% else %}
{{cat.product_subcategory.subcategory_name}}
{% endif %}    
</a>
</div><!-- End .product-cat -->
<h3 class="product-title"><a href="/products/category/{{cat.product_category.category_url}}/{{cat.product_subcategory.subcategory_url}}/{{cat.product_url}}">{{cat.product_model}}</a></h3><!-- End .product-title -->
<div class="product-price">

{% if cat.discount_price %}
<span class="new-price">{{cat.discount_price|intcomma}} تومان</span>
{% else %} 
<span class="price">{{cat.price|intcomma}} تومان</span>
{% endif %} 

</div><!-- End .product-price -->
<div class="ratings-container">
{% ratings cat %}
</div><!-- End .rating-container -->
</div><!-- End .product-body -->
</div><!-- End .product -->
</div><!-- End .col-sm-6 col-lg-4 col-xl-3 -->
{% endfor %}
</div><!-- End .row -->
</div><!-- End .products -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if products.number|add:'-4' > 1 %}
<li class="page-item">
<a class="page-link" href="?page={{ products.number|add:'-5' }}" aria-label="Previous" tabindex="-1" aria-disabled="true">
<span aria-hidden="true"></span><<
</a>
</li>
{% endif %}

{% if products.has_previous %}
<li class="page-item ">
<a class="page-link page-link-prev" href="?page={{ products.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}&{{ reqget.urlencode }}" aria-label="Previous" tabindex="-1" aria-disabled="true">
<span aria-hidden="true"><i class="icon-long-arrow-right"></i></span>قبلی
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link page-link-prev" href="" aria-label="Previous" tabindex="-1" aria-disabled="true">
<span aria-hidden="true"><i class="icon-long-arrow-right"></i></span>قبلی
</a>
</li>
{% endif %}
{% for i in products.paginator.page_range %}
{% if products.number == i %}
<li class="page-item active" aria-current="page"><a class="page-link" href="">{{ i }}</a></li>
{% elif i > products.number|add:'-5' and i < products.number|add:'4' %}
<li class="page-item" aria-current="page"><a class="page-link" href="?page={{ i }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}&{{ reqget.urlencode }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if products.has_next %}
<li class="page-item">
<a class="page-link page-link-next" href="?page={{ products.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}&{{ reqget.urlencode }}" aria-label="Next">
بعدی <span aria-hidden="true"><i class="icon-long-arrow-left"></i></span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link page-link-next" href="" aria-label="Next">
بعدی <span aria-hidden="true"><i class="icon-long-arrow-left"></i></span>
</a>
</li>
{% endif %}
{% if products.paginator.num_pages > products.number|add:'4' %}
<li class="page-item">
<a class="page-link" href="?page={{ products.number|add:'5' }}" aria-label="Previous" tabindex="-1" aria-disabled="true">
<span aria-hidden="true"></span>>>
</a>
{% endif %}
</ul>
</nav>
</div><!-- End .col-lg-9 -->
<aside class="col-lg-3 order-lg-first">
<div class="sidebar sidebar-shop">
<div class="widget widget-clean">
<label>فیلترها : </label>
<a href="#" class="sidebar-filter-clear">پاک کردن همه</a>
</div><!-- End .widget widget-clean -->
<div class="widget widget-collapsible">
<h3 class="widget-title">
<a data-toggle="collapse" href="#widget-1" role="button" aria-expanded="true" aria-controls="widget-1">
دسته بندی
</a>
</h3><!-- End .widget-title -->
<div class="collapse show" id="widget-1">
<div class="widget-body">
<div class="filter-items filter-items-count">
<form method="GET" action="?">
{% for subcategory in subcategories %}
<div class="filter-items filter-items-count">  
<div class="filter-item">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" value="{{subcategory.id}}" name="subcategory" id="{{subcategory.id}}" {% if subcategory in subcategories1 %} checked {% endif %}>
<label class="custom-control-label" for="{{subcategory.id}}">{{subcategory}}</label>
</div><!-- End .custom-checkbox -->
<span class="item-count">{{subcategory.products.all|length}}</span>
</div><!-- End .filter-item -->
</div><!-- End .filter-items -->
{% endfor %}
<button type="submit" class="btn btn-primary w-100 mt-3">فیلتر کن</button>
</form>
</div><!-- End .filter-items -->
</div><!-- End .widget-body -->
</div><!-- End .collapse -->
</div><!-- End .widget -->
</div><!-- End .sidebar sidebar-shop -->
</aside><!-- End .col-lg-3 -->
</div><!-- End .row -->
</div><!-- End .container -->
</div><!-- End .page-content -->
</main><!-- End .main -->
{% endblock content %}
{% block scripts %}
<script>
const filterBtn = document.getElementById('filterBtn');
const filter = document.getElementById('filter');
filterBtn.addEventListener('click', () =>{
filter.submit()
})
</script>
{% endblock scripts %}

我的整个html文件和视图,我得到相同的类别查询查询字符串像?page=2&category=3&category=1&category=2&category=3,当我打印reqget显示我<QueryDict: {'page': ['1'], 'category': ['3', '1', '2','3']}>每当我点击下一页或上一页时,关于过滤的问题,url不清楚,只是在前面的字符串后面添加相同的东西。它是正确的显示,但发送给视图的列表就像:['6','6','6','6','6','6','6','5','6']我认为这是因为使用检查选择。但我不提交它们,只是录制下一页或上一页。

您应该从链接到上一页、下一页或另一页的链接中删除{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}部分。链接如下:

<a href="?page={{ i }}&{{reqget.urlencode}}">…</a>

因此,您应该删除您自己实现的逻辑来呈现键值对,因为这是reqget.urlencode()所做的。

最新更新