11
class Product(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField()
    unit_price = models.DecimalField(max_digits=5, decimal_places=2)

I'am newbie to Django. How to design shopping basket using session? (ask for a general "algorithm" or some example code)

4

1 回答 1

15

我不会使用模型。您可以将值直接存储在会话中。考虑到您可以将所有内容存储在会话中,您可以将项目存储在字典中,执行类似的操作。

def view_cart(request):
    cart = request.session.get('cart', {})
    # rest of the view

def add_to_cart(request, item_id, quantity):
    cart = request.session.get('cart', {})
    cart[item_id] = quantity
    request.session['cart'] = cart
    # rest of the view
于 2012-11-20T01:49:37.903 回答