[Answer]-Django logout_then_login with next param

1👍

From the docs (Scroll down for this method)

logout_then_login(request[, login_url, current_app, extra_context])

Logs a user out, then redirects to the login page.

URL name: No default URL provided

Optional arguments:

  • login_url: The URL of the login page to redirect to. Defaults to settings.LOGIN_URL if not supplied.
  • current_app: A hint indicating which application contains the current view. See the namespaced URL resolution strategy for more information.
  • extra_context: A dictionary of context data that will be added to the default context data passed to the template.

Here we can see that the login method will accept a next parameter as a get variable.

Adding next to the login_url should do what you want:

from django.conf import settings
...
def myView(request):
    login_url = "%s?next=%s" % (settings.LOGIN_URL, request.path)
    logout_then_login(request, login_url=login_url)

Leave a comment