Login as any user from the Django admin interface, then switch back when done
Project description
Login as any user from the Django admin interface, then switch back when done
Authored by Adam Charnock, and some great contributors.
Installation
Either checkout django_su from GitHub, or install using pip :
pip install django-su
Add django_su to your INSTALLED_APPS. Make sure you put it before django.contrib.admin :
INSTALLED_APPS = ( ... 'django_su', # must be before ``django.contrib.admin`` 'django.contrib.admin', )
Add SuBackend to AUTHENTICATION_BACKENDS :
AUTHENTICATION_BACKENDS = ( ... 'django_su.backends.SuBackend', )
Update your urls.py file :
urlpatterns = [ 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.
django-su requires Django 1.5 or above.
External dependencies (optional, but recommended)
The following apps are optional but will enhance the user experience:
The ‘login su’ form will render using django-form-admin
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')}
Configuration (optional)
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 = "/"
# A function specifying the permissions a user requires in order
# to use the django-su functionality.
# Default: None
SU_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: None
SU_CUSTOM_LOGIN_ACTION = "example.utils.custom_login"
Usage
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.
How to
How to Notify superuser when connected with another user
This option warns the superuser when working with another user as initially logged in. To activate this option perform:
Add django_su.context_processors.is_su to TEMPLATE_CONTEXT_PROCESSORS :
TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django_su.context_processors.is_su', )
In your base.html include su/is_su.html snippet :
{% 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, your ModelAdmin in your admin.py file will need tweaking as follows:
# Within your admin.py file
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from . import models
@admin.register(models.CustomUser)
class CustomUserAdmin(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.
Credits
This app was put together by Adam Charnock, but was largely based on ideas, code and comments at:
django-su is packaged using seed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-su-0.8.0.tar.gz
.
File metadata
- Download URL: django-su-0.8.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8c9e74a033f9f33b22db011b82e56f0ec3d4f7bc0f699a14c54fc2981cbfe50 |
|
MD5 | 6a5eae1ab5993649e4dee90712b7c2a8 |
|
BLAKE2b-256 | 00666eb6fa8007136bdf4c73917cef989a8723eafe941513fa18fd5ea87a1437 |
Provenance
File details
Details for the file django_su-0.8.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_su-0.8.0-py2.py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa1f0d87a67bd768177542470eb62a58cff8fce3b92244d98b8de5fefbeb35db |
|
MD5 | 10c468b8912c146339bc75084bb28457 |
|
BLAKE2b-256 | 72947ad294cb3239bb1d3798ba4d1e5a40b50ee841fae3c843869fa982206aff |