I have been working with cookies. so watched the docs and some blogs and code the cookies just like below-->
def ProouctDetailView(request, slug):
item = Item.objects.get(slug=slug)
response = render(request, 'product.html', {'item' : item } )
temp = request.COOKIES.get('pro', None)
response.set_cookie('pro','{0} {1}'.format(temp, slug))
return response
Here I am getting the cookies. As django doesn't provide setting multipe values in a single cookie, I am getting all values together as a string(seperated by a space) and spliting them using split() method. Then I filter to generate the queryset for recently viewed section
def HomeListView(request):
items = Item.objects.all()
recent = None
try:
slug = request.COOKIES.get('pro').split(" ")
recent = Item.objects.filter(slug__in= slug)
except:
pass
return render(request, 'home.html', {'items': items, 'recent':recent})
From cookies,What I am getting are slugs of the items object so that later I can show them in recently viewed section as i didn't provide any max_age field in my set_cookie() method it will set as none which means this cookie will auto expire when user closes the browser session, means shuts down the browser,Right?
But the recently viewed section doesn't expires even if I shut down the chrome and restart the chrome. It still there. so the cookie didn't expired in chrome . But if I use firefox then the cookies expire when I restart the firefox.
Cookie should expire immediately once its expiration date is set by the server to a date in the past.
Cookie is still considered valid until the browser is restarted.
https://bugs.chromium.org/p/chromium/issues/detail?id=113073