How to clear cookies using Django

Nick Dong picture Nick Dong · Jan 18, 2013 · Viewed 22k times · Source

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?

Answer

Dawn T Cherian picture Dawn T Cherian · Sep 3, 2015

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