是根据这一个教程做的
http://www.cnblogs.com/holbrook/archive/2012/03/02/2357343.html
view_cart.html
{% extends "base.html" %} {% block title %} 我的购物车{% endblock %} {% block pagename %} 我的购物车 {% endblock %} {% block content %}
数量 | 名称 | 单价 | 小计 | ||
---|---|---|---|---|---|
{{item.quantity}} | {{item.product.title}} | {{item.unit_price}} | {% widthratio item.quantity 1 item.unit_price %} | ||
总计: | {{cart.total_price}} | 数量: | {% for item in cart.items %}{{item.quantity}}{% endfor %} |
{% endblock %}
views.py :
def view_cart(request): cart = request.session.get("cart",None) t = get_template('depotapp/view_cart.html') if not cart: cart = Cart() request.session["cart"] = cart c = RequestContext(request,locals()) return HttpResponse(t.render(c))
models.py
class Cart(object): def __init__(self, *args, **kwargs): self.items = [] self.total_price = 0 def add_product(self,product): self.total_price += product.price for item in self.items: if item.product.id == product.id: item.quantity += 1 return self.items.append(LineItem(product=product,unit_price=product.price,quantity=1))
请问一下,你现在还有这个教程的源代码吗?我现在也在根据这个教材做,可是也遇到了好多问题
你的 add_product 错了,return 的位置不对
def add_product(self,product): self.total_price += product.price for item in self.items: if item.product.id == product.id: item.quantity += 1 return self.items.append(LineItem(product=product,unit_price=product.price,quantity=1))