I am getting this error when i click on any item detail page. Generic detail view must be called with either an object pk or a slug in the URLconf.
URL:
path('item/<slug:item_slug>/', ItemDetailView.as_view(), name='item_detail'),
VIEW:
class ItemDetailView(DetailView):
model = Item
slug_field = 'item_slug'
MODEL:
class Item(models.Model):
title = models.CharField(max_length=100)
description= RichTextField(blank=True, null=True)
main_image= models.ImageField(null=True, blank=True,upload_to='images/')
date = models.DateTimeField(auto_now_add=True)
item_category = models.ForeignKey(Categories, default='Coding', on_delete=SET_DEFAULT)
slug = models.SlugField(unique=True, blank=True, null=True) # new
def save(self, *args, **kwargs):
if not self.slug and self.title:
self.slug = slugify(self.title)
super(Item, self).save(*args, **kwargs)
def __str__(self):
return self.title
The URL parameter is specified with the slug_url_kwarg
[Django-doc], not the . The slug_field
slug_field
[Django-doc] is used to filter the queryset, since you use slug
for your model, you do not need to change that.
So the view should look like:
class ItemDetailView(DetailView):
model = Item
slug_url_kwarg = 'item_slug'