The Django Admin Site

For most modern Web sites, an admin interface is an essential part of the infrastructure. This is a Web-based interface, limited to trusted site administrators, that enables the adding, editing and deletion of site content. Some common examples: the interface you use to post to your blog, the backend site managers use to moderate user-generated comments, the tool your clients use to update the press releases on the Web site you built for them.

There’s a problem with admin interfaces, though: it’s boring to build them. Web development is fun when you’re developing public-facing functionality, but building admin interfaces is always the same. You have to authenticate users, display and handle forms, validate input, and so on. It’s boring, and it’s repetitive.

So what’s Django’s approach to these boring, repetitive tasks? It does it all for you.

With Django, building an admin interface is a solved problem. In this chapter we will be exploring Django’s automatic admin interface: checking out how it provides a convenient interface to our models,
and some of the other useful things we can do with it.

Using the Django Admin Site

When you ran django-admin startproject mysite in Chapter 1, Django created and configured the default admin site for you. All that you need to do is create an admin user (superuser) and then you can log into the admin site.

To create an admin user, run the following command:

python createsuperuser 

Enter your desired username and press enter.

Username: admin 

You will then be prompted for your desired email address:

Email address: 

The final step is to enter your password.
You will be asked to enter your password twice, the second time as a confirmation of the first.

Password: **********
Password (again): *********
Superuser created successfully.

Start the Development Server

In Django 1.8, the django admin site is activated by default. Let’s start the development server and explore it. Recall from previous chapters that you start the development server like so:

python runserver 

Now, open a Web browser and go to /admin/ on your local domain – e.g., You should see the admin’s login screen
(Figure 5-1).

Django Admin Site - Django admin login screen
Figure 5-1: Django admin login screen

Since translation is turned on by default, the login screen may be displayed in your own language, depending on your browser’s settings and on whether Django has a translation for this language.

Enter the Admin Site

Now, try logging in with the superuser account you created in the previous step. You should see the Django admin index page (Figure 5-2).

You should see two types of editable content: groups and users. They are provided by django.contrib.auth, the authentication framework shipped by Django. The admin site is designed to be used by nontechnical users, and as such it should be pretty self-explanatory. Nevertheless, we’ll give you a quick walkthrough of the basic features.

Django Admin Site - Django admin home page
Figure 5-2: Django admin home page

Each type of data in the Django admin site has a change list and an edit form. Change lists show you all the available objects in the database, and edit forms let you add, change or delete particular records in your database. Click the “Change” link in the “Users” row to load the change list page for users (Figure 5-3).

Django Admin Site - The user change list page
Figure 5-3: The user change list page

This page displays all users in the database; you can think of it as a prettied-up Web version of a SELECT * FROM auth_user; SQL query. If you’re following along with our ongoing example, you’ll only see one user here, assuming you’ve added only one, but once you have more users, you’ll probably find the filtering, sorting and searching options useful.

Filtering options are at right, sorting is available by clicking a column header, and the search box at the top lets you search by username. Click the username of the user you created, and you’ll see the edit form for that user (Figure 5-4).

This page lets you change the attributes of the user, like the first/last names and various permissions. Note that to change a user’s password, you should click “change password form” under the password field rather than editing the hashed code.

Another thing to note here is that fields of different types get different widgets – for example, date/time fields have calendar controls, Boolean fields have checkboxes, character fields have simple text input fields.

Django Admin Site - The user edit form
Figure 5-4: The user edit form

You can delete a record by clicking the delete button at the bottom left of its edit form. That’ll take you to a confirmation page, which, in some cases, will display any dependent objects that will be deleted, too. (For example, if you delete a publisher, any book with that publisher will be deleted, too!)

You can add a record by clicking “Add” in the appropriate column of the admin home page. This will give you an empty version of the edit page, ready for you to fill out.

You’ll also notice that the admin interface also handles input validation for you. Try leaving a required field blank or putting an invalid date into a date field, and you’ll see those errors when you try to save, as shown in Figure 5-5.

When you edit an existing object, you’ll notice a History link in the upper-right corner of the window. Every change made through the admin interface is logged, and you can examine this log by clicking the History link (see Figure 5-6).

Django Admin Site - An edit form displaying errors
Figure 5-5: An edit form displaying errors
Django Admin Site - An object history page
Figure 5-6: An object history page