我想要做的是更新現有的已存在的django cookie。我正在創建一個添加到購物車功能,但無法在現有Cookie中添加更多項目。任何幫助都可以稱讚。 我的代碼是:Django現有cookie未更新
def AddToCart(request):
id = request.POST.get('id')
product = Product.objects.get(pk=id)
response = render(request,'index.html')
if request.COOKIES.get('product'):
request.COOKIES['product'] = {'name':product.name,'price':product.price,'img':product.image.url}
else:
response.set_cookie('product',{'name':product.name,'price':product.price,'img':product.image.url},
max_age= 14 * 24 * 60 * 60)
return response
非常感謝你。我需要爲這種情況使用會話。乾杯。 Upvoted。 –