按类别过滤以列出ListView Django时出现错误分页

时间:2019-06-03 03:12:23

标签: django

我是编程的新手,我的导师仅是youtube,当我遇到错误时,我在stackoverflow中搜索了类似的错误,但现在没有运气了。
我的代码或逻辑有问题。我没有关于正确过滤呼叫查询集的经验或知识。

这是我的urls.py

urlpatterns = [
     path('', IndexView.as_view(), name='index'),
     path('product/<int:page>', ProductListView.as_view(), name='allproducts'),
     path('<categories>/<int:page>', ProductCategoryListView.as_view(), 
     name='product_category'),
]

我有没有错误分页的ProductListView。

class ProductListView(ListView):
    context_object_name = 'product_list'
    template_name = 'product.html'
    model = Product
    paginate_by = 16

    def get_context_data(self, *args, **kwargs):
        context = super(ProductListView, self).get_context_data(*args,**kwargs)
        context ['catlist'] = Category.objects.all()
        return context
  • 按类别过滤时,分页不起作用。过滤 类别工作正常,但是当paginate_by超过我的产品时,页面 错误
  

找不到带有参数'('',2)'的'product_category'的反向。尝试了1个模式:['(?P [^ /] +)/(?P [0-9] +)$'] \

class ProductCategoryListView(ListView):
    context_object_name = 'product_list'
    template_name = 'product_list.html'
    model = Product
    paginate_by = 20

    def get_queryset(self):
        self.queryset = self.model.objects.filter(category__name=self.kwargs['categories'])
        return super().get_queryset()

    def get_context_data(self, *args, **kwargs):
        context = super(ProductCategoryListView, self).get_context_data(*args,**kwargs)
        context ['catlist'] = Category.objects.all()
        return context

这是我的models.py

class Category(models.Model):
    name = models.CharField(max_length=125)
    image = models.ImageField(upload_to=upload_location)

class Product(models.Model):
    name = models.CharField(max_length=125)
    category = models.ForeignKey(Category, null=True, on_delete=models.DO_NOTHING)
    image = models.ImageField(upload_to=upload_lokasi)
    slug = models.SlugField(blank=True, editable=False)
    active = models.BooleanField(default=True)

这是我ProductCategoryListView的模板

<div class="...">
            <div class="...">
                <a href="{% url 'products:allproducts' 1%}"...>
                    All Products
                </a>
                {% for cat in catlist %}
                <a href="{% url 'products:product_category' cat.name 1 %}"...>
                    {{ cat.name }}
                </a>
                {% endfor %}
                        <!-- Product List -->
        <div class="row isotope-grid">
            {% for prods in product_list %}
            <div class="...">
                <div class="...">
                    <div class="...">
                        <img src="{{ prods.image.url }}" alt="IMG-PRODUCT">
                        <a href="../{{ prods.slug }}" class="...">
                            Quick View
                        </a>
                    </div>
                    <div class="...">
                        <div class="...">
                            <a href="product-detail.html" class="...">
                                    {{ prods.name }}
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>

        <!-- Pagination -->
        <div class="row">
            <div class="col-md-8">
                {% if is_paginated %}
                    <nav aria-label="productPage">
                        <ul class="pagination">
                        {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="{% url 'products:product_category' category page_obj.previous_page_number %}">Previous</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a>
                            </li>
                        {% endif %}
                        {% for page in paginator.page_range %}
                            {% if page is page_obj.number %}
                            <li class="page-item active" aria-current="page">
                                <span class="page-link" href="#">{{page}}<span class="sr-only">(current)</span></span>
                            </li>
                            {% else %}
                            <li class="page-item">
                                <a class="page-link" href="{% url 'products:product_category' category page %}">{{page}}</a>
                            </li>
                            {% endif %}
                        {% endfor %}
                        {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{% url 'products:product_category' category page_obj.next_page_number %}">Next</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Next</a>
                            </li>
                        {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>

我猜错误来自这里,导致逻辑使用不同的模型。

def get_context_data(self, *args, **kwargs):
            context = super(ProductCategoryListView, self).get_context_data(*args,**kwargs)
            context ['catlist'] = Category.objects.all()
            return context

我的模板

{% for cat in catlist %}
    <a href="{% url 'products:product_category' cat.name 1 %}"...>
        {{ cat.name }}
    </a>
{% endfor %}
  • 我使用Category.objects.all()是因为我不知道如何列出 产品中的类别,只需访问类别名称即可。

我希望你们能理解我的要求。 对不起,我的英语不好。

0 个答案:

没有答案