This tutorial begins where Tutorial 1 left off. We’re continuing the Web-poll application and will focus on Django’s automatically-generated admin site.
Philosophy Generating admin sites for your staff or clients to add, change and delete content is tedious work that doesn’t require much creativity.
For that reason, Django entirely automates creation of admin interfaces for models. Django was written in a newsroom environment, with a very clear separation between “content publishers” and the “public” site. Site
managers use the system to add news stories, events, sports scores, etc., and that content is displayed on the public site. Django solves
the problem of creating a unified interface for site administrators to edit content. The admin isn’t intended to be used by site visitors. It’s for site managers.
Creating an admin user
First we’ll need to create a user who can login to the admin site. Run the following command:
$ python manage.py createsuperuser
Enter your desired username and press enter.
You will then be prompted for your desired email address:
Email address: firstname.lastname@example.org
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
The Django admin site is activated by default. Let’s start the development server and explore it.
Recall from Tutorial 1 that you start the development server like so:
$ python manage.py runserver
Now, open a Web browser and go to “/admin/” on your local domain – e.g., http://127.0.0.1:8000/admin/. You should see the admin’s login screen:
Doesn’t match what you see? If at this point, instead of the above login page, you get an error page reporting something like:
ImportError at /admin/ cannot import name patterns ...
then you’re probably using a version of Django that doesn’t match this tutorial version. You’ll want to either switch to the older tutorial
or the newer Django version.
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:
You should see a few types of editable content: groups and users. They are provided by django.contrib.auth, the authentication framework shipped by Django.
Make the poll app modifiable in the admin
But where’s our poll app? It’s not displayed on the admin index page.Just one thing to do: we need to tell the admin that Question objects have an admin interface. To do this, open the polls/admin.py file, and edit it to look like this:
from django.contrib import admin from .models import Question admin.site.register(Question)
Explore the free admin functionality
Now that we’ve registered Question, Django knows that it should be displayed on the admin index page:Click “Questions”. Now you’re at the “change list” page for questions. This page displays all the questions in the database and lets you choose one to change it. There’s the “What’s up?” question we created in the first tutorial: Click the “What’s up?” question to edit it: Things to note here:
- The form is automatically generated from the Question model.
- The different model field types (DateTimeField, CharField) correspond to the appropriate HTML input widget. Each type of field knows how to display itself in the Django admin.
- Save – Saves changes and returns to the change-list page for this type of object.
- Save and continue editing – Saves changes and reloads the admin page for this object.
- Save and add another – Saves changes and loads a new, blank form for this type of object.
- Delete – Displays a delete confirmation page.
Customize the admin form
Take a few minutes to marvel at all the code you didn’t have to write. By registering the Question model with
admin.site.register(Question), Django was able to construct a default form representation. Often, you’ll want to customize how the admin
form looks and works. You’ll do this by telling Django the options you want when you register the object.
Let’s see how this works by re-ordering the fields on the edit form. Replace the admin.site.register(Question) line with:
from django.contrib import admin from .models import Question class QuestionAdmin(admin.ModelAdmin): fields = ['pub_date', 'question_text'] admin.site.register(Question, QuestionAdmin)
You’ll follow this pattern – create a model admin object, then pass it as the second argument to admin.site.register() – any time you need to
change the admin options for an object.This particular change above makes the “Publication date” come before the “Question” field: