Me gustaría deshabilitar el parámetro ?next=... que Django Admin establece automáticamente si intenta acceder a una página que está protegida por el panel de administración. No he podido encontrar una solución para hacer esto hasta ahora. ¿Alguien sabe como lograr esto?
La razón por la que quiero hacer esto es para evitar ataques de enumeración de páginas.
Encontré la respuesta yo mismo después de un poco de prueba y error.
Necesitaba crear mi AdminSite personalizado y luego proporcionar mi propia admin_view personalizada, que realiza la redirección. Luego, en la redirección, acabo de configurar redirect_field_name en None así:
def admin_view(self, view, cacheable=False): """ Decorator to create an admin view attached to this ``AdminSite``. This wraps the view and provides permission checking by calling ``self.has_permission``. You'll want to use this from within ``AdminSite.get_urls()``: class MyAdminSite(AdminSite): def get_urls(self): from django.urls import path urls = super().get_urls() urls += [ path('my_view/', self.admin_view(some_view)) ] return urls By default, admin_views are marked non-cacheable using the ``never_cache`` decorator. If the view can be safely cached, set cacheable=True. """ def inner(request, *args, **kwargs): if not self.has_permission(request): if request.path == reverse('admin:logout', current_app=self.name): index_path = reverse('admin:index', current_app=self.name) return HttpResponseRedirect(index_path) # Inner import to prevent django.contrib.admin (app) from # importing django.contrib.auth.models.User (unrelated model). from django.contrib.auth.views import redirect_to_login return redirect_to_login( request.get_full_path(), reverse('admin:login', current_app=self.name), redirect_field_name=None # <-- Set this to None to disable the "?next=" parameter. ) return view(request, *args, **kwargs) if not cacheable: inner = never_cache(inner) # We add csrf_protect here so this function can be used as a utility # function for any view, without having to repeat 'csrf_protect'. if not getattr(view, 'csrf_exempt', False): inner = csrf_protect(inner) return update_wrapper(inner, view)