Unified LoginView/LogoutView.get_default_redirect_url() methods.

This might change the behavior when self.next_page == "". However,
resolve_url(self.next_page) would almost certainly fail in that case.

It is technically possible to define a logout URLpattern whose name is
"": path('logout/', LogoutView.as_view(), name=''), and then to refer to
this pattern with next_page = "". However this feels like a pathological
case, so we decided not to handle it.

Most checks on next_page, LOGIN_REDIRECT_URL, and LOGOUT_REDIRECT_URL
are performed with boolean evaluation rather than comparison with None.
That's why we standardizing that way.
This commit is contained in:
Aymeric Augustin 2022-04-16 19:36:59 +02:00 committed by Mariusz Felisiak
parent 5b8699e723
commit 5fcd9b8c33
1 changed files with 13 additions and 6 deletions

View File

@ -85,7 +85,10 @@ class LoginView(RedirectURLMixin, FormView):
def get_default_redirect_url(self):
"""Return the default redirect URL."""
return resolve_url(self.next_page or settings.LOGIN_REDIRECT_URL)
if self.next_page:
return resolve_url(self.next_page)
else:
return resolve_url(settings.LOGIN_REDIRECT_URL)
def get_form_class(self):
return self.authentication_form or self.form_class
@ -151,13 +154,17 @@ class LogoutView(RedirectURLMixin, TemplateView):
# RemovedInDjango50Warning.
get = post
def get_success_url(self):
if self.next_page is not None:
next_page = resolve_url(self.next_page)
def get_default_redirect_url(self):
"""Return the default redirect URL, or None if no URL is configured."""
if self.next_page:
return resolve_url(self.next_page)
elif settings.LOGOUT_REDIRECT_URL:
next_page = resolve_url(settings.LOGOUT_REDIRECT_URL)
return resolve_url(settings.LOGOUT_REDIRECT_URL)
else:
next_page = self.next_page
return None
def get_success_url(self):
next_page = self.get_default_redirect_url()
if (
self.redirect_field_name in self.request.POST