HttpResponse.set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False)
Sets a cookie. The parameters are the same as in the Morsel cookie object in the Python standard library.
-
max_ageshould be a number of seconds, orNone(default) if the cookie should last only as long as the client’s browser session. Ifexpiresis not specified, it will be calculated. -
expiresshould either be a string in the format"Wdy, DD-Mon-YY HH:MM:SS GMT"or adatetime.datetimeobject in UTC. Ifexpiresis adatetimeobject, themax_agewill be calculated. - Use
domainif you want to set a cross-domain cookie. For example,domain=".lawrence.com"will set a cookie that is readable by the domains www.lawrence.com, blogs.lawrence.com and calendars.lawrence.com. Otherwise, a cookie will only be readable by the domain that set it. -
Use
httponly=Trueif you want to prevent client-side JavaScript from having access to the cookie.HTTPOnly is a flag included in a Set-Cookie HTTP response header. It is not part of the RFC 2109 standard for cookies, and it isn’t honored consistently by all browsers. However, when it is honored, it can be a useful way to mitigate the risk of a client-side script from accessing the protected cookie data.
Warning
Both RFC 2109 and RFC 6265 state that user agents should support cookies of at least 4096 bytes. For many browsers this is also the maximum size. Django will not raise an exception if there’s an attempt to store a cookie of more than 4096 bytes, but many browsers will not set the cookie correctly.
Please login to continue.