Delete certain cookies on logout
I am using some cookies to store the state of some sections. jQuery is used to set the cookies. I need to have the right path in order to delete the cookie. This code works but I've provided the value开发者_如何转开发s for the paths. Function len returns 0. How can I obtain the cookies with the path values?
def mylogout(request, next_page=None, template_name=None):
response = render_to_response(template_name,{
}, context_instance = RequestContext(request))
logout(request)
print len(response.cookies.items())
response.delete_cookie('section_body_1', path='/some/url/data/edit/48/')
response.delete_cookie('section_body_2', path='/some/url/data/edit/48/')
response.delete_cookie('section_body_3', path='/some/url/data/edit/48/')
return response
You can't. Server receives only name and value of the cookie. You can set additional cookie parameters only during cookie creation. After that, user agent is responsible for managing cookies.
Setting cookie without a path will implicitly use current url path as cookie path.
If it's not an issue, you can set constant, common path (i.e., /
) for your cookies, and then use it to delete them.
Unfortunately, cookies set with a path containing a specific subdirectory cannot be accessed by scripts outside of that subdirectory. Also, as cezio mentioned, you can't actually delete cookies on a user's computer anyway. Instead, the general practice is to set the cookie's expiration date to a date in the past (like 1/1/1900).
精彩评论