Skip to main content

No project description provided

Project description


django-switchuser makes it easy for an administrator to switch to temporarily switch to another account by visiting /su.


Because django-switchuser was a quick project, it does make one assumption:

  • If a user is not allowed to su, then they will get an HTTP 404 if they try to visit /su/ or do anything su-related.
  • Any superuser is allowed to switch to any other user. If this assumption does not hold: you’ll need to submit a pull request (hint: take a look at django_switchuser/… Sorry :(


Tested with Django 1.8 through 1.10, should work with earlier Django versions too.


  1. pip install django-switchuser

  2. Add a few things to (note: the SuStateMiddleware should be the very first class in the list; this guarantees that request.su_state will always be available):

  3. Add an entry to (note: you can use whatever URL you’d like; su/ is simply convenient):

    urlpatterns += patterns("",
        url(r"^su/", include("django_switchuser.urls")),
  4. Start the server and check that everything is working by visiting http://localhost:8000/su/ Note: an HTTP 404 will be returned if the currently logged in user isn’t allowed to su (by default, only administrators are allowed to su).

  5. (Optional) Add an entry to your base.html template which will show a convenient logout button:

            {% include "su/statusbar.html" %}
  6. (Optional) Override SuState so it better suits your application. For example, to include fields from a user’s profile, you subclass SuState like this (see below for more detailed documentation):

    from django.contrib.auth.models import User
    from django_switchuser.state import SuState as DefaultSuState
    class SuState(DefaultSuState):
        def available_users(self):
            return User.objects.all()\
        def user_long_label(self, user):
            return "%s (%s)" %(user.get_profile().client_id, user.username)
        def user_short_label(self, user):
            return "%s" %(user.get_profile().client_id, )

    And then add to your file:


Doing Your Own Thing

Doing your own thing is easy. The SuStateMiddleware and su_state context processors add a su_state attribute to the request and a su_state variable to the template rendering context. su_state is an instance of django_switchuser.state.SuState, and has the following attributes:

Returns True if the current user has been switched.
The original user associated with the request. For example, if the user admin has switched to jane, then su_state.auth_user will be admin.
The user which has been switched to, or None if no user has been switched. For example, if the user admin has switched to jane, then su_state.active_user will be admin.
Returns True if the current user is allowed to switch.

Returns a QuerySet of User of the users which the current user is allowed to switch to. It may be useful to override this method to select_related() on the user’s profile:

def available_users(self):
    return User.objects.all()\

Returns the “long” label for the user, used in the list of users. It may be useful to override this method so that it includes information specific to your application:

def user_long_label(self, user):
    return "%s (%s)" %(user.get_profile().client_id, user.username)

Returns the “short” label for the user, used in the status bar and other places. It may be useful to override this method so that it includes information specific to your application:

def user_short_label(self, user):
    return "%s" %(user.get_profile().client_id, )
Switches to the user with id su_user_id if they are included in SuState.available_users().
Switches to User su_user. No permissions checks are performed.
Reverts back to the original user.

For example, if you don’t like the default switch user bar, you could add your own to your base.html:

{% load switchuser %}
{% load url from future %}

{% if su_state.is_active %}
    <a href="{% url "su-logout" %}">deactive {% su_user_short_label su.active_user %}</a>
{% elif su_state.can_su %}
    <a href="{% url "su-login" %}">switch user</a>
{% endif %}

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-switchuser-0.6.1.tar.gz (8.0 kB view hashes)

Uploaded source

Built Distribution

django_switchuser-0.6.1-py2-none-any.whl (12.9 kB view hashes)

Uploaded 2 7

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page