我正在创建一个用于库存管理的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
答案 0 :(得分:0)
404可能是因为您在URL中有一个空格字符被编码为%20,然后与正则表达式的\w
部分不匹配。