Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django logout(redirect to home page) .. Delete cookie?

Tags:

cookies

django

I redirect the user to the home page after logout. In between I would like to delete all/or specific client cookies (I have previously set).

def logoutuser(request):
    logout(request)
    return redirect('app.home.views.home')

To call response.delete_cookie('user_location'), there is no response object. How do I do this?

like image 398
Ramya Avatar asked Aug 14 '09 00:08

Ramya


3 Answers

Like jobscry said, logout() cleans session data, but it looks like you have set your own cookies too.

You could wrap auth logout view, which will return a HttpResponse:

def logout_user(request):
     response = logout(request, next_page=reverse('app.home.views.home'))
     response.delete_cookie('user_location')
     return response

Or if you're just using the logout method as opposed to the view, you can use the return value for the redirect() method you have (which I assume returns a HttpResponse too).

def logout_user(request):
     logout(request)
     response = redirect('app.home.views.home')
     response.delete_cookie('user_location')
     return response
like image 122
SmileyChris Avatar answered Nov 13 '22 14:11

SmileyChris


according to http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.logout

Changed in Django 1.0: Calling logout() now cleans session data.

like image 31
imjoevasquez Avatar answered Nov 13 '22 16:11

imjoevasquez


Hope this helps: delete cookie when caling "/clear-cookies"

location.href = '/clear-cookies';
  1. Define a method in views.py:

    def clear_cookies(request):
        response = HttpResponseRedirect('/')
        response.delete_cookie('_gat', domain='example.com')
        response.delete_cookie('_ga', domain='example.com')
        response.delete_cookie('_gid', domain='example.com')
        return response
    
  2. Add the path and method to your urls.py:

    url(r'^clear-cookies', clear_cookies),
    
like image 1
2567910 Avatar answered Nov 13 '22 15:11

2567910