dev-toolbar a web developer's best friend!
The dev-toolbar is a simple concept we came up with many years ago. The idea is to provide developers with a toolbelt full of tricks (shortcuts, toggles, login-as…) hidden in the frontend. It is such an integral part of my workflow that it is usually the first thing I set up in any project.
In this entry we will see how to set it up in a Django web application. We will focus on the feature that everybody loves: the ability to login as any user in the database (great time saver). We call it impersonate , and this is how it looks like:
A simple select with all the users in your database. Click on one and you’ll be logged in as him/her.
Let’s get on with it!
1. Create a dev app
We will start by creating a new app that we will call dev
. It will hold all of our impersonation logic, but is also a useful place to have to keep a style guide for developers, or some handy snippets to share accross the team, or maybe just to house a temporary experiment.
and don’t forget to add it to your INSTALLED_APPS
2. The view and urls
Here we will control if the current user is allowed to impersonate or not, and perform the actual impersonation. In this case we are allowing any superuser to login as any other user, but you may choose any other criteria, for example only in development environment, or only if DEBUG
is True
.
3. Template tag
We want to reuse our toolbar across many templates so we will extract it into it’s own template tag, using Django’s inclusion-tags functionality.
For that you need to create this directory: ./dev/templatetags
and an empty __init__.py
file inside of it.
Inside our templatetags folder we will create the following file, which acts like the view of our new dev_toolbar
templatetag. Notice 2 things:
- First we are linking it to the template
dev/toolbar.html
that we will define later. - And second we use
takes_context=True
to be able to access to the current user. This will allow us to do some basic authorization (ie. only superusers are allowed to use this feature). This is redundant with the logic we defined before, but it’s good to have a safety net when you’re handling delicate data.
The template file contains its own css, javascript and html. Some simple positioning and styling and bit of css+js logic to make the bar more subtle.
We will render a select box with all the users in the database and a javascript will take care of calling the impersonate action whenever we choose another user other than ourselves. Two more handy links to /dev
and /admin
areas are included.
Last, but not least, I usually include a small close button, it comes in handy sometimes when doing CSS work, you just want to get rid of the bar.
4. Using it
Our feature is complete and ready to be used. Simply load it in any view and call it.
{% load dev_toolbar %}
{% dev_toolbar %}
5. Show it in the admin panel
We are going to override the default admin/base.html
template and include our new dev_toolbar
snippet. For that we need to create a generic ./templates
directory at the root of the project and add it to TEMPLATES['DIRS']
in the settings of the project.
And now create a new file to override the default administration base tempalte.