django.urls.exceptions.NoReverseMatch: Reverse for 'detail' with keyword arguments '{'slug': 'test'}' not found. 1 pattern(s) tried: ['detail<slug:slug/\\Z']
I'm trying to create a product detail, filtering it by slug(maybe it can be ID), so I decided to send the slug trough an URL and then filter the product by the specifiedslug, but im getting troubles to implement it(my other get_absolute_url its working but this one I cant fix).
models.py:
class Product(models.Model):
name = models.CharField(max_length=255, unique=False, blank=True)
slug = models.SlugField(unique=True)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, unique=False)
subcategory = models.ForeignKey(
SubCategory, on_delete=models.CASCADE, unique=False, blank=True, null=True)
short_description = models.CharField(
max_length=255, unique=False, blank=True, null=True)
long_description = models.TextField(unique=False, blank=True, null=False)
image = models.ImageField(blank=True, null=True, unique=False)
on_promotion = models.BooleanField(default=False)
def __str__(self):
return self.slug
def get_absolute_url(self):
return reverse("product:detail", kwargs={"slug": self.slug})
urls.py:
urlpatterns = [
path('', views.index, name="index"),
path('detail<slug:slug/', views.ProductDetail.as_view(), name="detail"),
path('category-<int:category_id>/', views.category, name="category"),
path('category-<int:category_id>/subcategory-<int:subcategory_id>',
views.subcategory, name="subcategory")
]
views.py """
def detail(request, product_id):
product = Product.objects.get(product_id)
return render(request, 'product/detail.html', {'product_id': product})
"""
class ProductDetail(DetailView):
template_name = 'product/detail.html'
# queryset = Product.available.all()
def get_queryset(self):
queryset = Product.available.filter(slug=self.kwargs.get("slug"))
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
template:
<a href="{{product.get_absolute_url}}">aaa</a>
CodePudding user response:
path('detail<slug:slug/', views.ProductDetail.as_view(), name="detail"),
the path is wrong, you forgot to put the closing > after slug
'detail<slug:slug>/'
and i am not sure whether you wrote app_name = "product" or not in urls.py
CodePudding user response:
I think you need to remove product:
in the get_absolute_url
method. If you need to add the app namespace (which you don't for your current case), you should use the current_app
argument as specified here: https://docs.djangoproject.com/en/4.1/topics/http/urls/#topics-http-reversing-url-namespaces
In addition to the above, you could also use {% url 'detail' product.slug %}
CodePudding user response:
i am not sure about your main urls.py
, but i know: to use named urls concatenated with ':' you should provide url-namespace.
https://docs.djangoproject.com/en/4.1/topics/http/urls/#url-namespaces
please check in settings - main urls.py
:
path('products', include('products.urls', namespace='products))
After that you can use:
def get_absolute_url(self):
return reverse("products:detail", current_app=self.request.resolver_match.namespace, kwargs={"slug": self.slug})
In your case you should omit 'product:'
def get_absolute_url(self):
return reverse('detail', kwargs={"slug": self.slug})