NoReverseMatch at / category / clothes /基于Django类的视图

时间:2016-11-05 16:29:48

标签: django django-models django-urls django-class-based-views slug

我有一个 django 项目,在功能方面接近电子商务wesite。

彼此有四页链接。首页显示类别,第二子类别,第三产品列表和第四产品详情,我正在使用 slugs 导航。

错误 Reverse for 'product-list' with arguments '('', 'women-clothes')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['category/(?P<category_slug>[-\\w]+)/(?P<subcategory_slug>[-\\w]+)/$']

category_list.html 上的子类别链接代码的类别为<a href="{% url 'products-app:sub-category' category.category_slug %}">{{ category.name }}</a> views.py

class CategoryListView(ListView):
    models = Category
    template_name = 'products/category_list.html'
    context_object_name = "Category list"

    def get_queryset(self):
        """
        Returns all categories.
        """
        return Category.objects.get_queryset().all()

urls.py

app_name = 'products'

urlpatterns = [
 url(r'^$', CategoryListView.as_view(), name='categories'),
 url(r'^(?P<category_slug>[-\w]+)/$', SubcategoryListView.as_view(), name='sub-category'),
 url(r'^(?P<category_slug>[-\w]+)/(?P<subcategory_slug>[-\w]+)/$', ProductListView.as_view(), name='product-list'),
 url(r'^(?P<category_slug>[-\w]+)/(?P<subcategory_slug>[-\w]+)/(?P<pk>\d+)/$', ProductDetailView.as_view(), name='product-detail'),] 

问题是将 subcategory_list.html product_list 相关联。因为我需要传递 category_slug subcategory_slug

<a href="{% url 'products-app:product-list' category_slug subcategory_slug %}">{{ object.name }}</a>

我不知道如何使用cbv来实现这个逻辑。我想传递 category_slug ,因为它来自类别模型并从子类别模型查询。 的 views.py

class SubcategoryListView(ListView):
    """
    Browse all products in the sub-catalogue.
    """
    model = Subcategory
    template_name = 'products/subcategory_list.html'
    context_object_name = "Sub-Category list"
    category_model = Category

    def get_queryset(self):
        """
        Returns all sub-categories.
        """
        self.category = get_object_or_404(Category, category_slug = self.kwargs.get('category_slug'))
        return Subcategory.objects.filter(category = self.category)

category.html 有效。

    {% for category in object_list %}
    <div class="col-xs-12 col-md-12">
        <a href="{% url 'products-app:sub-category' category.category_slug %}">{{ category.name }}</a>
        <p>{{ category.category_slug }}</p>
    </div>
    {% endfor %}

subcategory.html

        {% for object in object_list %}
        <div class="col-xs-12 col-md-12">
            <a href="{% url 'products-app:product-list' object.category_slug object.subcategory_slug %}">{{ object.name }}</a>
         <p>subcategory_slug:{{ object.subcategory_slug }}</p>
        </div>
       {% endfor %}

如何获取 category_slug 并在上面的视图中传递它,以便我可以在模板上迭代它们?

2 个答案:

答案 0 :(得分:1)

我真的不知道这与CBV有什么关系。您没有显示很多模板,但可能您正在迭代子类别并希望链接到该子类别的单个列表页面。因此,您只需要在循环中传递slug和当前子类别的类别slug。

如果您展示模板的其余部分和模型,这会更容易,但假设object是子类别,并且字段名为“subcategory_slug”,而SubCategory模型的FK为Category:

<a href="{% url 'products-app:product-list' object.category.category_slug object.subcategory_slug %}">{{ object.name }}</a>

答案 1 :(得分:-1)

我能够解决这个错误。 的更改

<强> views.py

class SubcategoryListView(ListView):
    """
    Browse all products in the sub-catalogue.
    """
    model = Subcategory
    template_name = 'products/subcategory_list.html'
    context_object_name = "Sub-Category list"
    category_model = Category

    def get_queryset(self):
        """
        Returns all sub-categories.
        """
        self.category = get_object_or_404(Category, category_slug = self.kwargs.get('category_slug'))
        return Subcategory.objects.filter(category = self.category)

    def get_context_data(self, **kwargs):
        """
        Returns self.category_slug needed 
        on the subcategory_list.html as a
        one of the {% url %} slug params.
        """
        context = super(SubcategoryListView, self).get_context_data(**kwargs)
        context['categories'] = Category.objects.all()
        context['category_slug'] = self.kwargs.get('category_slug')
        return context

subcategory_list.html 上,我将 object.category_slug 更改为 category_slug

<强> subcategory_list.html <a href="{% url 'products-app:product-list' category_slug object.subcategory_slug %}">{{ object.name }}</a>