To set cookies with Python Django, we can use set_cookie
and request.COOKIES
.
For instance, we write
def view(request):
response = HttpResponse('blah')
response.set_cookie('cookie_name', 'cookie_value')
to call response.set_cookie
with the cookie name and value.
And then we get the cookie by writing
def view(request):
value = request.COOKIES.get('cookie_name')
if value is None:
# Cookie is not set
to call request.COOKIES.get
with the cookie name to get the cookie.