diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index b540fbc5a321f..8eef00f2c1d7f 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -489,6 +489,8 @@ def gettext_noop(s): LOGIN_REDIRECT_URL = '/accounts/profile/' +LOGOUT_REDIRECT_URL = None + # The number of days a password reset link is valid for PASSWORD_RESET_TIMEOUT_DAYS = 3 diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py index fee3067646e87..e2387fea89eb9 100644 --- a/django/contrib/auth/views.py +++ b/django/contrib/auth/views.py @@ -104,6 +104,8 @@ def logout(request, next_page=None, if next_page is not None: next_page = resolve_url(next_page) + elif settings.LOGOUT_REDIRECT_URL: + next_page = resolve_url(settings.LOGOUT_REDIRECT_URL) if (redirect_field_name in request.POST or redirect_field_name in request.GET): diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 2101c1c7d44bb..ab710fffac954 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -2642,6 +2642,26 @@ This setting also accepts view function names and :ref:`named URL patterns ` which can be used to reduce configuration duplication since you don't have to define the URL in two places (``settings`` and URLconf). +.. setting:: LOGOUT_REDIRECT_URL + +``LOGOUT_REDIRECT_URL`` +----------------------- + +.. versionadded:: 1.10 + +Default: ``None`` + +The URL where requests are redirected after a user logs out using the +:func:`~django.contrib.auth.views.logout` view (if the view doesn't get a +``next_page`` argument). + +If ``None``, no redirect will be performed and the logout view will be +rendered. + +This setting also accepts view function names and :ref:`named URL patterns +` which can be used to reduce configuration duplication +since you don't have to define the URL in two places (``settings`` and URLconf). + .. setting:: PASSWORD_RESET_TIMEOUT_DAYS ``PASSWORD_RESET_TIMEOUT_DAYS`` diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 92f7c1d8e60ff..7329a6717d35e 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -82,6 +82,10 @@ Minor features * Added the optional ``backend`` argument to :func:`~django.contrib.auth.login` to allow using it without credentials. +* The new :setting:`LOGOUT_REDIRECT_URL` setting controls the redirect of the + :func:`~django.contrib.auth.views.logout` view, if the view doesn't get a + ``next_page`` argument. + :mod:`django.contrib.contenttypes` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/auth/default.txt b/docs/topics/auth/default.txt index 5b6472f7a3ab7..690d11e3b857b 100644 --- a/docs/topics/auth/default.txt +++ b/docs/topics/auth/default.txt @@ -1105,7 +1105,9 @@ implementation details see :ref:`using-the-views`. **Optional arguments:** - * ``next_page``: The URL to redirect to after logout. + * ``next_page``: The URL to redirect to after logout. Defaults to + :setting:`settings.LOGOUT_REDIRECT_URL ` if not + supplied. * ``template_name``: The full name of a template to display after logging the user out. Defaults to diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index f3593d46c5be3..76a1abf6ed797 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -878,6 +878,18 @@ def test_logout_preserve_language(self): self.client.get('/logout/') self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], 'pl') + @override_settings(LOGOUT_REDIRECT_URL='/custom/') + def test_logout_redirect_url_setting(self): + self.login() + response = self.client.get('/logout/') + self.assertRedirects(response, '/custom/', fetch_redirect_response=False) + + @override_settings(LOGOUT_REDIRECT_URL='logout') + def test_logout_redirect_url_named_setting(self): + self.login() + response = self.client.get('/logout/') + self.assertRedirects(response, '/logout/', fetch_redirect_response=False) + # Redirect in test_user_change_password will fail if session auth hash # isn't updated after password change (#21649)