Django cookies

setting cookies


    from django.shortcuts import render
    
    response = HttpResponse()
    response = render(request, 'hr/index.html',{'leaveRequests': leaveRequests,})
    response.set_cookie(key='sampCookieKey', value=1)
    return response  


syx:-  set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False)

max_age should be a number of seconds, or None (default) if the cookie should last only as long as the client’s browser session. If expires is not specified, it will be calculated.

expires should either be a string in the format "Wdy, DD-Mon-YY HH:MM:SS GMT" or a datetime.datetime object in UTC. If expires is a datetime object, the max_age will be calculated.

Use domain if 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=True if you want to prevent client-side JavaScript from having access to the cookie.


======================================================

Signed cookie
set_signed_cookie(key, value, salt='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=True


=========================================================

getiing cookies value

request.COOKIES.get('sampCookieKey')

==========================================================
deleting cookies 

request.delete_cookie(key, path='/', domain=None)


Author: bm on July 8, 2014
Category: Python Django

Your comment:

Your Name

Comment:




Last articles