基于 Django 类的视图和通用视图详细信息使用
Posted
技术标签:
【中文标题】基于 Django 类的视图和通用视图详细信息使用【英文标题】:Django Class based view and generic view details usage 【发布时间】:2013-12-13 01:47:41 【问题描述】:我正在编写一个用于库存管理的 django 应用程序。我希望 django 处理如下所示的网址
"foo:8000/stores/Electrical Store/" 其中“Electrical Store”是由一个视图(另一个视图)生成的,该视图显示商店列表。 http 响应是使用 ListView 通用视图生成的 - 也可以使用模板视图生成,我尝试了不同的替代方案,同时尝试熟悉基于类的视图的工作原理。
我的问题是,当尝试让 django 呈现带有“商店详细信息”的 http 页面和商店中的产品列表时,我在下面收到 404 错误:
^stores/(?P\w+)/$ [name='stores_product'] 当前 URL,Electrical Store/,与其中任何一个都不匹配。
而且我不确定我的代码哪里出了问题——基本上,文档没有提供足够的信息或示例来展示他们试图传达的概念。我在下面提供了相关的模型和视图。请帮忙...
# models
# PRODUCT MODEL
class Product(models.Model):
# Desription - ID
name = models.CharField(primary_key = True, unique = True, max_length = 100)
description = models.TextField(max_length = 200)
# Amount per containment unit
amount_per_containment_unit = models.IntegerField("Unit Count", default = 0)
def __unicode__(self):
# return unicode(self.datetime)
return unicode(self.name)
class Meta:
ordering = ["name"]
# STORE MODEL
# A Store has a name, an address, a phone number, a group of users who act as curators and a list of stock
# items.
class Store(models.Model):
name = models.CharField(primary_key = True, max_length = 100)
address = models.CharField(max_length = 100)
phone = models.CharField(max_length = 100)
curators = models.ManyToManyField(User, verbose_name = "Curator", blank = True, null = True)
products = models.ManyToManyField(Product, through = "StoreProduct", blank = True, null = True)
# Curator list
def curator_list(self):
return ",\n".join([obj.first_name for obj in self.curators.all()])
def __unicode__(self):
# return unicode(self.datetime)
return unicode(self.name)
class Meta:
ordering = ["name"]
# StoreProduct MODEL
class StoreProduct(models.Model):
store = models.ForeignKey(Store)
product = models.ForeignKey(Product)
quantity = models.IntegerField(default = 0)
total = models.IntegerField(default = 0)
class Meta:
unique_together = (("store", "product"),)
def __init__(self, *args, **kwargs):
super(StoreProduct, self).__init__(*args, **kwargs)
if self.total == None and self.store and self.product and not self.pk:
self.total = self.product.amount_per_containment_unit * self.quantity
def save(self, *args, **kwargs):
if self.store and self.product and self.quantity:
self.total = self.product.amount_per_containment_unit * self.quantity
super(StoreProduct, self).save(*args, **kwargs)
# views
class StoreList(TemplateView):
# Template
template_name = "inventory/store/store_list.html"
# Context
def get_context_data(self, **kwargs):
context = super(StoreList, self).get_context_data(**kwargs)
context["store_list"] = Store.objects.all()
return context
class StoreProductList(TemplateView):
template_name = "inventory/store/store_products.html"
# Context
def get_context_data(self, **kwargs):
# Objects
self.store_inst = Store.objects.get(pk = self.kwargs["name"])
self.queryset = StoreProduct.objects.filter(store = self.store_inst)
print self.store_inst
context = super(StoreList, self).get_context_data(**kwargs)
# Store primary key
context["store"] = self.store_inst
context["store_products"] = self.queryset
【问题讨论】:
【参考方案1】:404 可能是因为 URL 中有一个空格字符被编码为 %20,这与正则表达式的 \w
部分不匹配。
【讨论】:
以上是关于基于 Django 类的视图和通用视图详细信息使用的主要内容,如果未能解决你的问题,请参考以下文章
如何根据 Django 中当前基于类的通用视图模型向模板加载器添加路径
Django REST framwork-03-使用mixin和基于类的通用(generics)视图