{"id":3966,"library":"django-loginas","title":"Django Login As","description":"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.","status":"active","version":"0.3.14","language":"en","source_language":"en","source_url":"https://github.com/skorokithakis/django-loginas/","tags":["django","admin","authentication","impersonation","superuser","staff"],"install":[{"cmd":"pip install django-loginas","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This is a Django application and requires a compatible Django version. It officially supports Django 2.2 through 6.0.","package":"Django"}],"imports":[{"note":"Add 'loginas' to your Django project's INSTALLED_APPS setting.","symbol":"INSTALLED_APPS","correct":"INSTALLED_APPS = [\n    # ...\n    'loginas',\n    # ...\n]"},{"note":"For Django 3.2 and newer, `loginas.urls` must be included *before* `admin.site.urls` in your project's `urls.py`.","wrong":"urlpatterns = [\n    path('admin/', admin.site.urls),\n    path('admin/', include('loginas.urls')),\n]","symbol":"urlpatterns","correct":"from django.urls import path, include\nfrom django.contrib import admin\n\nurlpatterns = [\n    path('admin/', include('loginas.urls')), # MUST be before admin.site.urls for Django 3.2+\n    path('admin/', admin.site.urls),\n    # ...\n]"},{"note":"If using a custom User model, configure its Admin class to use the `loginas` change form template, or place an `extends 'loginas/change_form.html'` template at `templates/<app_label>/<model_name>/change_form.html`.","symbol":"ModelAdmin (for custom User models)","correct":"from django.contrib import admin\nfrom django.contrib.auth.admin import UserAdmin\nfrom .models import CustomUser\n\n@admin.register(CustomUser)\nclass CustomUserAdmin(UserAdmin):\n    change_form_template = 'loginas/change_form.html'"}],"quickstart":{"code":"# settings.py\nINSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'loginas', # Add this line\n]\n\n# urls.py\nfrom django.contrib import admin\nfrom django.urls import path, include\n\nurlpatterns = [\n    path('admin/', include('loginas.urls')), # Important: Must be before admin.site.urls for Django 3.2+\n    path('admin/', admin.site.urls),\n    # Your other URL patterns\n]\n\n# Optional: admin.py (if using a custom User model)\n# from django.contrib import admin\n# from django.contrib.auth.admin import UserAdmin\n# from .models import CustomUser # Replace with your custom User model\n\n# @admin.register(CustomUser)\n# class CustomUserAdmin(UserAdmin):\n#     change_form_template = 'loginas/change_form.html'\n\n# To run the example:\n# 1. Create a Django project and app if you don't have one.\n# 2. Add 'loginas' to INSTALLED_APPS.\n# 3. Add loginas URLs as shown above.\n# 4. python manage.py migrate\n# 5. python manage.py createsuperuser (create an admin user)\n# 6. python manage.py runserver\n# 7. Log into Django admin, navigate to a user, and the 'Log in as user' button will be visible.","lang":"python","description":"To get started with django-loginas, first install the package. Then, add 'loginas' to your `INSTALLED_APPS` in `settings.py`. Crucially, include `loginas.urls` in your project's `urls.py` *before* `admin.site.urls` to ensure proper routing, especially for Django 3.2 and later. If you are using a custom User model, you may need to explicitly set the `change_form_template` for its `ModelAdmin` to `loginas/change_form.html`."},"warnings":[{"fix":"Upgrade to Python 3.x to use versions 0.3.7+ or pin django-loginas to 0.3.6 for Python 2 projects.","message":"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.","severity":"breaking","affected_versions":"< 0.3.7"},{"fix":"Ensure `path('admin/', include('loginas.urls'))` appears before `path('admin/', admin.site.urls)` in your `urlpatterns`.","message":"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.","severity":"breaking","affected_versions":">= 0.3.0 with Django >= 3.2"},{"fix":"Temporarily remove superuser status from the target user before logging in as them, then restore it afterwards.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade django-loginas to version 0.3.13 or newer to ensure compatibility with Django 6.0 and avoid deprecation warnings/potential breakage related to admin logging.","message":"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.","severity":"deprecated","affected_versions":"< 0.3.13 with Django 6.0+"},{"fix":"Upgrade to django-loginas 0.3.12 or later if you are using a strict Content Security Policy in your Django project.","message":"Version 0.3.12 introduced compatibility with strict Content Security Policy (CSP) script-src definitions. Older versions might experience issues with stricter CSP configurations.","severity":"gotcha","affected_versions":"< 0.3.12 with strict CSP"},{"fix":"Update any references to `MESSAGE_LOGIN_REVERT` in your code to `LOGINAS_MESSAGE_LOGIN_REVERT`.","message":"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.","severity":"gotcha","affected_versions":"< 0.3.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}