Login as any user from the Django admin interface, then switch back when done
Authored by Adam Charnock, and some great contributors.
- Either checkout
django_su
from GitHub, or install usingpip
:
pip install django-su
- Add
django_su
to yourINSTALLED_APPS
. Make sure you put it beforedjango.contrib.admin
:
INSTALLED_APPS = (
...
'django_su', # must be before ``django.contrib.admin``
'django.contrib.admin',
)
- Add
SuBackend
toAUTHENTICATION_BACKENDS
:
AUTHENTICATION_BACKENDS = (
...
'django_su.backends.SuBackend',
)
- Update your
urls.py
file :
urlpatterns = patterns('',
url(r'^su/', include('django_su.urls')),
...
)
And that should be it!
Please see example
application. This application is used to manually test the functionalities of this package. This also serves as a good example.
You need only Django 1.4 or above to run that. It might run on older versions but that is not tested.
If you install these two apps the enhance user experience:
- The 'login su' form will render using django admin forms
- The user selection widget will render using django ajax selects
Note that django ajax selects requires the following settings:
AJAX_LOOKUP_CHANNELS = {'django_su': dict(model='auth.user', search_field='username')}
There are various optional configuration options you can set in your settings.py
# URL to redirect after the login.
# Default: "/"
SU_LOGIN_REDIRECT_URL = "/"
# URL to redirect after the logout.
# Default: "/"
SU_LOGOUT_REDIRECT_URL = "/dashboard/"
# A function to specify the perms that the user must have can use django_su
# Default: None
SU_LOGIN_CALLBACK = "example.utils.su_login_callback"
# A function to override the django.contrib.auth.login(request, user)
# function so you can set session data, etc.
# Default: None
SU_CUSTOM_LOGIN_ACTION = "example.utils.custom_login"
Go 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.
This 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.html
- http://copiousfreetime.blogspot.com/2006/12/django-su.html
django-su is packaged using seed.