I am receiving that error when I try to visit the detail page of my product model. I have the slug field in the url file, but it doesn't seem to matter.
Model
class Product(models.Model):
product_name= models.CharField(max_length=30, blank=False, null=False, verbose_name="the product name")
product_slug= models.SlugField(max_length=30, blank=False, null=False, verbose_name="the product slug")
product_excerpt= models.CharField(max_length=100, blank=False, null=False, verbose_name="product excerpt")
def _set_product_code(self):
product_code_temp = hashlib.sha224()
product_hash = self.product_name
product_hash = product_hash.encode('utf-8')
product_code_temp.update(product_hash)
return product_code_temp.hexdigest()[0:5]
product_code = property(_set_product_code)
View
class ProductPage(DetailView):
model = Product
context_object_name = 'product'
template_name="product.html"
Url
url(r'^product/(?P<product_slug>\w+)/(?P<product_code>\w+)/$', ProductPage.as_view(), name="product"),
Can anyone pinpoint what I'm doing wrong?
I would like to add that get_object could also be used in a case where we have a different look up parameter for primary key
Example, if I want to get my object by guid, which I am passing in my URLConf
def get_object(self):
return Product.objects.get(guid=self.kwargs.get("guid"))
Set the slug_field
attribute on the view class:
class ProductPage(DetailView):
model = Product
slug_field = 'product_slug'
Depending on your URLConf you may also need to specify the name of the kwarg that corresponds to the slug. It defaults to 'slug'. If you used something different in the URL specification, such as 'product_slug' in this example, specify the slug_url_kwarg
property on the view as well:
slug_url_kwarg = 'product_slug'
# etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With