Ioh*_*nes 8 django url templates model view
我尝试访问产品型号的详细信息页面时收到该错误.我在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)
Run Code Online (Sandbox Code Playgroud)
视图
class ProductPage(DetailView):
model = Product
context_object_name = 'product'
template_name="product.html"
Run Code Online (Sandbox Code Playgroud)
网址
url(r'^product/(?P<product_slug>\w+)/(?P<product_code>\w+)/$', ProductPage.as_view(), name="product"),
Run Code Online (Sandbox Code Playgroud)
任何人都可以找出我做错了什么吗?
Pet*_*per 16
slug_field
在视图类上设置属性:
class ProductPage(DetailView):
model = Product
slug_field = 'product_slug'
# etc
Run Code Online (Sandbox Code Playgroud)
我想补充一点,get_object也可用于我们为主键提供不同查找参数的情况
例如,如果我想通过guid获取我的对象,我将在URLConf中传递它
def get_object(self):
return Product.objects.get(guid=self.kwargs.get("guid"))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14227 次 |
最近记录: |