Django
2
Вклад в тег
def add_product_to_cart(request):
if request.method == 'POST':
id = int(request.POST['product_id'])
if 'cart' not in request.session:
request.session['cart'] = []
request.session['cart'].append(int(id))
request.session.modified = True
return redirect("/")
def show_cart(request):
cart = request.session.get('cart')
product_items = Product.objects.filter(id__in=cart) #можно и так Product.objects.filter(id__in=request.session['cart'])
content={
"product_items":product_items
}
return render (request, "orderdetail.html", content)
def add_product_to_cart(request):
if request.method == 'POST':
id = int(request.POST['product_id'])
price = int(request.POST['price'])
if 'cart' not in request.session:
request.session['cart'] = []
request.session['cart'].append(dict({'product_id': id, 'price': price}))
request.session.modified = True
return redirect("/")
def show_cart(request):
cart = request.session.get('cart')
product_items = Product.objects.filter(id__in=(x['product_id'] for x in cart) )#можно прописать и для price вот только фактически это не нужно...
content={
"product_items":product_items
}
return render (request, "orderdetail.html", content)