Package: django-su
Login as any user from the Django admin interface, then switch back when doneAuthored byAdam Charnock, and some greatcontributors.InstallationEither checkoutdjango_sufrom GitHub, or install usingpip:pipinstalldjango-suAdddjango_suto yourINSTALLED_APPS. Make sure you put itbeforedjango.contrib.admin:INSTALLED_APPS=(...'django_su',# must be before ``django.contrib.admin``'django.contrib.admin',)AddSuBackendtoAUTHENTICATION_BACKENDS:AUTHENTICATION_BACKENDS=(...'django_su.backends.SuBackend',)Update yoururls.pyfile :urlpatterns=[url(r'^su/',include('django_su.urls')),...]And that should be it!Please seeexampleapplication. This application is used to manually test
the functionalities of this package. This also serves as a good example.django-suis tested on Django 2.2 or above, lower versions may work, but are considered unsupported.External dependencies (optional, but recommended)The following apps are optional but will enhance the user experience:The ‘login su’ form will render usingdjango-form-adminThe user selection widget will render usingdjango-ajax-selectsNote thatdjango-ajax-selectsrequires the following settings:AJAX_LOOKUP_CHANNELS={'django_su':dict(model='auth.user',search_field='username')}Configuration (optional)There are various optional configuration options you can set in yoursettings.py# URL to redirect after the login.# Default: "/"SU_LOGIN_REDIRECT_URL="/"# URL to redirect after the logout.# Default: "/"SU_LOGOUT_REDIRECT_URL="/"# A function specifying the permissions a user requires in order# to use the django-su functionality.# Default: NoneSU_LOGIN_CALLBACK="example.utils.su_login_callback"# A function to override the django.contrib.auth.login(request, user)# view, thereby allowing one to set session data, etc.# Default: NoneSU_CUSTOM_LOGIN_ACTION="example.utils.custom_login"UsageGo and view a user in the admin interface and look for a new “Login
as” button in the top right.Once you have su’ed into a user, you can get exit back into your
original user by navigating to/su/in your browser.How toHow to Notify superuser when connected with another userThis option warns the superuser when working with another user as
initially logged in. To activate this option perform:Adddjango_su.context_processors.is_sutoTEMPLATE_CONTEXT_PROCESSORS:TEMPLATE_CONTEXT_PROCESSORS=(...'django_su.context_processors.is_su',)In yourbase.htmlincludesu/is_su.htmlsnippet :{%include"su/is_su.html"%}How to use django-su with a custom user model (AUTH_USER_MODEL)Django-su should function normally with a custom user model. However,
yourModelAdminin youradmin.pyfile will need tweaking as follows:# Within your admin.py filefromdjango.contribimportadminfromdjango.contrib.auth.adminimportUserAdminfrom.importmodels@admin.register(models.CustomUser)classCustomUserAdmin(UserAdmin):# The following two lines are needed:change_form_template="admin/auth/user/change_form.html"change_list_template="admin/auth/user/change_list.html"This ensures the Django admin will use the correct template customisations for
your custom user model.CreditsThis app was put together by Adam Charnock, but was largely based on ideas, code and comments at:http://bitkickers.blogspot.com/2010/06/add-button-to-django-admin-to-login-as.htmlhttp://copiousfreetime.blogspot.com/2006/12/django-su.htmldjango-su is packaged usingseed.