Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear cookies using Django

I am trying to develop login page for a web site. I am using Django 1.4.2. I stored users which logged on correctly to a cookie using set_cookie. But I didn't find clear_cookie in Django's documentation. How to clear a cookie to make a user log out?

like image 308
Nick Dong Avatar asked Jan 18 '13 14:01

Nick Dong


People also ask

How to clear cookies in Django?

To delete a cookie, simply call response. delete_cookie('cookie_name') . There is no cookie update method in HttpResponse , use set_cookie() to update the cookie value or expiry time.

How do you delete cookies in Python?

Python Flask- Delete Cookies To delete a cookie call set_cookie() method with the name of the cookie and any value and set the max_age argument to 0.

What is session and cookies in Django?

Django provides a session framework that lets you store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side. So the data itself is not stored client side.


1 Answers

Setting cookies :

    def login(request):         response = HttpResponseRedirect('/url/to_your_home_page')         response.set_cookie('cookie_name1', 'cookie_name1_value')         response.set_cookie('cookie_name2', 'cookie_name2_value')         return response 

Deleting cookies :

    def logout(request):         response = HttpResponseRedirect('/url/to_your_login')         response.delete_cookie('cookie_name1')         response.delete_cookie('cookie_name2')         return response 
like image 169
Dawn T Cherian Avatar answered Sep 22 '22 21:09

Dawn T Cherian