NoReverseMatch:Django URL

时间:2017-11-19 04:40:42

标签: python django

我无法在ProductDetailView中显示产品。

ProductDetailView将空字符串返回到' category-detail' ..

在产品详细信息视图中,它链接到:

http://127.0.0.1:8000/products/products/6/

但返回此错误:

Reverse for 'category_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['products/categories/(?P<pk>\\d+)$']

信息在类别和产品的列表视图中显示正常。

urls.py

    url(r'^categories/(?P<pk>\d+)/$', views.CategoryDetailView.as_view(), name='category_detail'),

models.py

class Category(models.Model):
    """
    Model For a product category
    """

    c_name = models.CharField(max_length=200, help_text="Enter a Product Category: ")


    def __str__(self):
        """
        String Representation for the Model object
        """
        return self.c_name

    def get_absolute_url(self):
        """
        Return an absolute URL to access a product instance
        """
        return reverse('category_detail', args=[str(self.id)])   

views.py

class CategoryDetailView(generic.DetailView):
    template_name = 'category_detail.html'
    context_object_name = 'category_detail'
    paginate_by = 2
    model = Category

    def get_object(self):
        return get_object_or_404(self.model, pk=self.kwargs['pk'])

    def get_context_data(self, *args, **kwargs):
        context = super(CategoryDetailView, self).get_context_data(*args, **kwargs)
        context['products'] = self.get_object().products.all()
        return context


class ProductDetailView(generic.DetailView):
    model = Product
    context_object_name = 'product_detail'
    template_name = 'product_detail.html'
    paginate_by = 2

    def get_object(self):
        return get_object_or_404(self.model, pk=self.kwargs['pk'])    

    def get_context_data(self, *args, **kwargs):
        context = super(ProductDetailView, self).get_context_data(*args, **kwargs)
        return context    

HTML

  <p><strong>Category:</strong> <a href="{% url 'category_detail' product.category.pk %}">{{ product.category }}</a></p> 

我缺少什么?

2 个答案:

答案 0 :(得分:0)

我认为您的CategoryDetailView需要get_object

from django.shortcuts import get_object_or_404
...


class CategoryDetailView(generic.DetailView):
    template_name = 'category_detail.html'
    context_object_name = 'category_detail'
    paginate_by = 2
    model = Category

    def get_object(self):
        return get_object_or_404(self.model, pk=self.kwargs['pk'])

    def get_context_data(self, *args, **kwargs):
        context = super(CategoryDetailView, self).get_context_data(*args, **kwargs)
        context['products'] = self.get_object().products.all()
        return context

并更改网址;

url(r'^categories/(?P<pk>\d+)$', ...

为:

url(r'^categories/(?P<pk>\d+)/$', ...

答案 1 :(得分:0)

您需要取出上下文名称

class CategoryDetailView(generic.DetailView):
    ...
    context_object_name = 'category_detail' # 
     ...

class ProductDetailView(generic.DetailView):
    ...
    context_object_name = 'product_detail.html'
    ...

这些正在搞乱链接系统。

另一件事......在product_detail.html中。您需要将product.pk添加到网址

      <a href="{% url 'product-update' product.pk %}" class="btn btn-primary" role="button">update</a>

      <a href="{% url 'product-delete' product.pk %}" class="btn btn-danger" role="button">Delete</a>

这应该有用..

也可以在完成产品编辑后链接回产品详细信息视图:)