How to clear cookies using Django

烈酒焚心 提交于 2019-12-01 14:38:19

问题


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?


回答1:


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



回答2:


You can simply delete whatever you've stored in the cookie - this way, even though the cookie is there, it no longer contain any information required for session tracking and the user needs to authorize again.

(Also, this seems like a duplicate of Django logout(redirect to home page) .. Delete cookie?)



来源:https://stackoverflow.com/questions/14401398/how-to-clear-cookies-using-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!