当我使用代码{{ item.description }}
在django cartridge的 cart.html 页面上打印项目名称时,它还会打印项目名称尺寸.. 例如 aviesta是商品名称,尺寸是2.然后打印(aviesta尺寸:3) ... 如何打破此名称和大小分为两个不同的部分..
1.项目名称
2.物品尺寸
答案 0 :(得分:1)
我认为它需要更改模型,因为当产品添加到购物车时,名称和选项会保存到说明中:
class Cart(models.Model):
...
item.description = unicode(variation)
和
class ProductVariation(Priced):
...
def __unicode__(self):
"""
Display the option names and values for the variation.
"""
options = []
for field in self.option_fields():
if getattr(self, field.name) is not None:
options.append("%s: %s" % (unicode(field.verbose_name),
getattr(self, field.name)))
return ("%s %s" % (unicode(self.product), ", ".join(options))).strip()
UPD:
您可以向SelectedProduct类添加字段:
options = CharField(_("Options"), max_length=200)
将方法添加到ProductVariation类:
def options_text(self):
options = []
for field in self.option_fields():
if getattr(self, field.name) is not None:
options.append("%s: %s" % (unicode(field.verbose_name),
getattr(self, field.name)))
return ", ".join(options).strip()
def __unicode__(self):
"""
Display the option names and values for the variation.
"""
return ("%s %s" % (unicode(self.product), self.options_text())).strip()
更改Cart类中的add_item方法:
item.description = unicode(variation.product)
item.options = variation.options_text()