Philipp Mayr
Philipp Mayr

Reputation: 41

Django lockdown logout

Im trying to logout of my lockdown session. In the docs it says

LOCKDOWN_LOGOUT_KEY

A key which, if provided in the query string of a locked URL, will log out the user from the preview.

I'm not sure if I understand it rightly. I tried to implement this like this:

  1. I have the lockdown middleware in the middleware list.
  2. I have these lockdown options in settings.py:

LOCKDOWN_FORM = 'lockdown.forms.AuthForm'
LOCKDOWN_AUTHFORM_STAFF_ONLY = False
LOCKDOWN_LOGOUT_KEY = 'logout'

  1. I have a button which links to "/logout/"

<form action="/logout/">
  <input type="submit" value="Logout"/>
</form>

  1. This just links to a HttpResponseRedirect() back to my main page:

urls.py:

path('logout/', views.logout, name='logout')

views.py:

def logout(request):
	return HttpResponseRedirect("/")

The link works and takes me back to my main page. But the logout doesn't occure. Does anyone know how to do this?

EDIT: I found a solution. I added one line of code to the logout function in views.py:

def logout(request):
  request.session.flush()
  return HttpResponseRedirect("/")

Upvotes: 0

Views: 190

Answers (2)

Jeff Bowen
Jeff Bowen

Reputation: 6162

Just add the value of LOCKDOWN_LOGOUT_KEY ("preview-logout" by default) as a query string parameter.

<a href="/?preview-logout">Log out</a>

Upvotes: 0

Philipp Mayr
Philipp Mayr

Reputation: 41

I found a solution. I added one line of code to the logout function in views.py:

def logout(request):
  request.session.flush()
  return HttpResponseRedirect("/")

Upvotes: 0

Related Questions