当我尝试访问产品模型的详细信息页面时,我收到该错误。我在url文件中有slug字段,但这似乎无关紧要。
模型
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)
看法
class ProductPage(DetailView):
model = Product
context_object_name = 'product'
template_name="product.html"
网址
url(r'^product/(?P<product_slug>\w+)/(?P<product_code>\w+)/$', ProductPage.as_view(), name="product"),
谁能指出我做错了什么?
最佳答案
在 View 类上设置slug_field
属性:
class ProductPage(DetailView):
model = Product
slug_field = 'product_slug'
根据您的URLConf,您可能还需要指定与该段相对应的kwarg的名称。它默认为'slug'。如果您在URL规范中使用了其他内容,例如本示例中的“product_slug”,则还要在 View 上指定
slug_url_kwarg
属性: slug_url_kwarg = 'product_slug'
# etc
关于django - 通用详细信息 View 必须使用对象pk或子弹调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407239/