An app to add a "Log in as user" button in the Django user admin page.
Maintained. Last release 11 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install django-loginasdjango-loginas
About
"Login as user" for the Django admin.
loginas supports Python 3 only, as of version 0.4. If you're on 2, use
0.3.6.
Installing django-loginas
-
Add
loginasto your Python path, or install using pip:pip install django-loginas -
Add the
loginasapp to yourINSTALLED_APPS:
# settings.py
INSTALLED_APPS = [... 'loginas', ...]
- Add the
loginasURL to yoururls.py:
# urls.py
urlpatterns = [
# from Django 3.2 on, make sure to add loginas urls before the admin site urls, i.e.:
path('admin/', include('loginas.urls')),
path('admin/', admin.site.urls),
]
- If you're using a custom User model, you'll need to add the template to it so the button shows up:
# admin.py
class YourUserAdmin(ModelAdmin):
change_form_template = 'loginas/change_form.html'
Alternatively, you can add a change_form.html template containing just {% extends 'loginas/change_form.html' %}
to your project under the path expected by your custom User model, e.g.
.../templates/<app_label>/<model_name>/change_form.html.
At this point, you should be good to go. Just visit the Django admin, navigate to a user and you should see the "Log in as user" button at the top right of the screen.
Configuring
At this point, the only users who will be able to log in as other users are those with the is_superuser permission.
If you use custom User models, and haven't specified that permission, or if you want to change which users are
authorized to log in as others, you can define the CAN_LOGIN_AS setting, like so:
# settings.py
# This will only allow admins to log in as other users:
CAN_LOGIN_AS = lambda request, target_user: request.user.is_superuser
# This will only allow admins to log in as other users, as long as
# those users are not admins themselves:
CAN_LOGIN_AS = lambda request, target_user: request.user.is_superuser and not target_user.is_superuser
# You can also define a string path to a module:
CAN_LOGIN_AS = "utils.helpers.custom_loginas"