Package: django-loginas
django-loginasAbout"Login as user" for the Django admin.loginassupports Python 3 only, as of version 0.4. If you're on 2, use0.3.6.Installing django-loginasAddloginasto your Python path, or install using pip:pip install django-loginasAdd theloginasapp to yourINSTALLED_APPS:# settings.pyINSTALLED_APPS=[...'loginas',...]Add theloginasURL to yoururls.py:# urls.pyurlpatterns=[# from Django 3.2 on, make sure to add loginas urls before the admin site urls, i.e.:path('admin/',include('loginas.urls')),path('admin/',admin.site.urls),]If you're using a custom User model, you'll need to add the template to it so the button shows up:# admin.pyclassYourUserAdmin(ModelAdmin):change_form_template='loginas/change_form.html'At this point, you should be good to go. Just visit the Django admin, navigate to a user and you should see the "Log
in as user" button at the top right of the screen.ConfiguringAt this point, the only users who will be able to log in as other users are those with theis_superuserpermission.
If you use custom User models, and haven't specified that permission, or if you want to change which users are
authorized to log in as others, you can define theCAN_LOGIN_ASsetting, like so:# settings.py# This will only allow admins to log in as other users:CAN_LOGIN_AS=lambdarequest,target_user:request.user.is_superuser# This will only allow admins to log in as other users, as long as# those users are not admins themselves:CAN_LOGIN_AS=lambdarequest,target_user:request.user.is_superuserandnottarget_user.is_superuser# You can also define a string path to a module:CAN_LOGIN_AS="utils.helpers.custom_loginas"By default, clicking "Login as user" will send the user tosettings.LOGIN_REDIRECT_URL.
You can override this behavior like so:# settings.pyLOGINAS_REDIRECT_URL='/loginas-redirect-url'In order to automatically restore the original user upon log out, replace the default log out
with a special log out that restores the original login session from a signed session.# settings.pyfromdjango.core.urlresolversimportreverse_lazyLOGOUT_URL=reverse_lazy('loginas-logout')Additionally, you can specify the redirect url for logout (the default issettings.LOGIN_REDIRECT_URL).# settings.pyfromdjango.core.urlresolversimportreverse_lazyLOGINAS_LOGOUT_REDIRECT_URL=reverse_lazy('admin:index')By default, clicking "Login as user" will not updateuser.last_login.
You can override this behavior like so:# settings.pyLOGINAS_UPDATE_LAST_LOGIN=TrueBy default, the login switch message will generateDjango adminLogEntrymessages using theUsermodel'sUSERNAME_FIELDlikef"User {impersonator_user.getattr(USERNAME_FIELD)} logged in as {impersonated_user.getattr(USERNAME_FIELD)}."You can override this behavior by passing in a different
field name:# settings.pyLOGINAS_USERNAME_FIELD='email'Other implementation suggestionsExisting logout view?If you already have a logout view, you can modify to login the original user again after having had a "login as" session. Here's an example:classLogoutView(LogoutView):template_name='myapp/logged_out.html'@method_decorator(never_cache)defdispatch(self,request,*args,**kwargs):fromloginas.utilsimportrestore_original_loginrestore_original_login(request)returnredirect('myapp:login')Template awarenessYou can add the context processorloginas.context_processors.impersonated_session_statusin your settings.py file if you'd like to be able to access a variableis_impersonated_sessionin all your template contexts:# settings.pyTEMPLATES=[{...'OPTIONS':{'context_processors':['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth',...'loginas.context_processors.impersonated_session_status',],},},]Note that django-loginas won't let you log in as other superusers, to prevent
privilege escalation from staff users to superusers. If you want to log in as
a superuser, first demote them to a non-superuser, and then log in.LicenseThis software is distributed under the BSD license.