Django Login As
django-loginas is a Django app that integrates a "Log in as user" button directly into the Django user administration page. This allows superusers or authorized staff to quickly impersonate other user accounts for testing or support purposes. The library is currently at version 0.3.14 and maintains a regular release cadence with updates addressing bug fixes, new features, and Django version compatibility.
Warnings
- breaking Python 2 support was dropped in versions 0.3.7 and 0.3.8. If your project still relies on Python 2, you must use django-loginas version 0.3.6 or earlier.
- breaking For Django 3.2 and newer, the `loginas.urls` must be included in your `urls.py` *before* `admin.site.urls`. Failing to do so will prevent the 'Log in as user' button from working correctly.
- gotcha By default, django-loginas prevents superusers from logging in as other superusers to avoid privilege escalation. If you need this functionality, you must temporarily demote the target superuser.
- deprecated The `log_action` function, used for logging admin actions, was deprecated in Django 6.0. Version 0.3.13 of django-loginas includes a fix for this deprecation.
- gotcha Version 0.3.12 introduced compatibility with strict Content Security Policy (CSP) script-src definitions. Older versions might experience issues with stricter CSP configurations.
- gotcha The message constant `MESSAGE_LOGIN_REVERT` was renamed to `LOGINAS_MESSAGE_LOGIN_REVERT` in version 0.3.0. If you customized or referenced the old constant, it will no longer be found.
Install
-
pip install django-loginas
Imports
- INSTALLED_APPS
INSTALLED_APPS = [ # ... 'loginas', # ... ] - urlpatterns
from django.urls import path, include from django.contrib import admin urlpatterns = [ path('admin/', include('loginas.urls')), # MUST be before admin.site.urls for Django 3.2+ path('admin/', admin.site.urls), # ... ] - ModelAdmin (for custom User models)
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from .models import CustomUser @admin.register(CustomUser) class CustomUserAdmin(UserAdmin): change_form_template = 'loginas/change_form.html'
Quickstart
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'loginas', # Add this line
]
# urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', include('loginas.urls')), # Important: Must be before admin.site.urls for Django 3.2+
path('admin/', admin.site.urls),
# Your other URL patterns
]
# Optional: admin.py (if using a custom User model)
# from django.contrib import admin
# from django.contrib.auth.admin import UserAdmin
# from .models import CustomUser # Replace with your custom User model
# @admin.register(CustomUser)
# class CustomUserAdmin(UserAdmin):
# change_form_template = 'loginas/change_form.html'
# To run the example:
# 1. Create a Django project and app if you don't have one.
# 2. Add 'loginas' to INSTALLED_APPS.
# 3. Add loginas URLs as shown above.
# 4. python manage.py migrate
# 5. python manage.py createsuperuser (create an admin user)
# 6. python manage.py runserver
# 7. Log into Django admin, navigate to a user, and the 'Log in as user' button will be visible.