Django Impersonate

1.9.5 · active · verified Fri Apr 17

Django Impersonate is a Django application that allows superusers (or users with specific permissions) to temporarily assume the identity of another user. This is highly useful for debugging, support, or testing user experiences. The current version is 1.9.5, and it is actively maintained with regular releases aligning with Django and Python version compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up django-impersonate, add `impersonate` to `INSTALLED_APPS`, integrate `ImpersonateMiddleware` into your `MIDDLEWARE` stack (crucially, after `SessionMiddleware` and `AuthenticationMiddleware`), and include `impersonate.urls` in your project's `urls.py`. You can then use the provided views or template tags to initiate and end impersonation sessions. The quickstart demonstrates basic setup and how to include the URLs and middleware, along with template tag usage.

# settings.py
INSTALLED_APPS = [
    # ...
    'impersonate',
]

MIDDLEWARE = [
    # ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'impersonate.middleware.ImpersonateMiddleware', # Must be AFTER Session and Auth middleware
    # ...
]

# urls.py
from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('impersonate/', include('impersonate.urls')),
    # ... your other urls
]

# In a template (e.g., base.html) to show an impersonation link:
# {% load impersonate_tags %}
# {% if user.is_authenticated and user.is_superuser %}
#   {% if impersonate %} # 'impersonate' variable comes from context processor
#     <a href="{% url 'impersonate-leave' %}">Leave Impersonation</a>
#   {% else %}
#     <a href="{% url 'impersonate-start' user_id=1 %}">Impersonate User (ID 1)</a> # Replace 1 with actual user ID
#     <a href="{% url 'impersonate-start' username='testuser' %}">Impersonate User (username testuser)</a>
#   {% endif %}
# {% endif %}

view raw JSON →