Django Slug在类别中独一无二

时间:2015-02-05 10:24:50

标签: django listview slug django-class-based-views detailview

我想在Django中提出一个URL定义来支持这个

/类别/ categorie_slug /产品/ product_slug

其中,categorie_slug为Unique,而product_slug对于父类别是唯一的。

我的观点:

"""
Listviews
"""
class CategorieList(ListView):
    context_object_name = "categorie_list"
    model = Categorie
    queryset = Categorie.objects.order_by('-created_at')

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CategorieList, self).get_context_data(**kwargs)
        # Add in a QuerySet of all Products
        context['product_list'] = Product.objects.all()
        return context


class ProductList(ListView):
    context_object_name = "product_list"
    model = Product

    def get_queryset(self):
        categorie = self.kwargs['categorie']
        return Product.objects.filter(categorie=categorie)

"""
Detailviews
"""
class CategorieDetail(DetailView):
    context_object_name = "categorie"
    model = Categorie

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CategorieDetail, self).get_context_data(**kwargs)
        categorie = self.object
        # Add in a QuerySet of all posts for the given estimates
        context['product_list'] = Product.objects.filter(categorie=categorie)
        return context


class ProductDetail(DetailView):
    context_object_name = "product"
    model = Product

我的网址格式:

    urlpatterns = patterns('',
    url(r'^$', login_required(CategorieList.as_view()),name="categories"),
    url(r'^.*categories/$', login_required(CategorieList.as_view()),name="categories"),
    url(r'^.*categories/(?P<slug>[\w-]+)/$', login_required(CategorieDetail.as_view()), name='categorie'),

    url(r'^.*categories/(?P<slug>[\w-]+)/products/$', login_required(Productlist.as_view()), name="products"),
    url(r'^.*categories/(?P<slug>[\w-]+)/products/(?P<slug>[\w-]+)/$', login_required(ProductDetail.as_view()), name="product"),
)

问题在于我必须将categorie-slug作为第二个参数添加到我的Productdetail中,因为两个slug都使产品独一无二。

0 个答案:

没有答案