summaryrefslogtreecommitdiff
path: root/django/contrib/admin
diff options
context:
space:
mode:
authorBenedict Etzel <developer@beheh.de>2025-11-10 13:29:34 +0100
committernessita <124304+nessita@users.noreply.github.com>2025-11-12 19:42:24 -0300
commit5401b125abca53200eacb62c8a10e602359b76d4 (patch)
tree72bb904a1fb153fd27411fed0a14f41468d915e3 /django/contrib/admin
parent66b5a6de78ac3bcdf586844eac61663fece10ab5 (diff)
Fixed #36717 -- Redirect authenticated users on admin login view to next URL.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'django/contrib/admin')
-rw-r--r--django/contrib/admin/sites.py18
1 files changed, 8 insertions, 10 deletions
diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
index 17af19fd1b..410bf20da0 100644
--- a/django/contrib/admin/sites.py
+++ b/django/contrib/admin/sites.py
@@ -416,29 +416,27 @@ class AdminSite:
"""
Display the login form for the given HttpRequest.
"""
- if request.method == "GET" and self.has_permission(request):
- # Already logged-in, redirect to admin index
- index_path = reverse("admin:index", current_app=self.name)
- return HttpResponseRedirect(index_path)
-
# Since this module gets imported in the application's root package,
# it cannot import models from other applications at the module level,
# and django.contrib.admin.forms eventually imports User.
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth.views import LoginView
+ redirect_url = LoginView().get_redirect_url(request) or reverse(
+ "admin:index", current_app=self.name
+ )
+ if request.method == "GET" and self.has_permission(request):
+ # Already logged-in, redirect accordingly.
+ return HttpResponseRedirect(redirect_url)
+
context = {
**self.each_context(request),
"title": _("Log in"),
"subtitle": None,
"app_path": request.get_full_path(),
"username": request.user.get_username(),
+ REDIRECT_FIELD_NAME: redirect_url,
}
- if (
- REDIRECT_FIELD_NAME not in request.GET
- and REDIRECT_FIELD_NAME not in request.POST
- ):
- context[REDIRECT_FIELD_NAME] = reverse("admin:index", current_app=self.name)
context.update(extra_context or {})
defaults = {