The Django Admin Site
The admin site is a built-in control panel for a Django project. Staff use it to add and change rows through a web page.
Add the admin address
The admin address goes in the URL patterns. It points at the built-in admin site.
from django.contrib import admin
from django.urls import path
urlpatterns = [path("admin/", admin.site.urls)]Make an admin account
This command asks for a name and a password. Run it in a terminal.
python manage.py createsuperuserAsk for the admin page
The test client asks for /admin/ without logging in. The admin answers with a redirect to the login page.
client = Client()
print(client.get("/admin/").status_code)Log in and ask again
Login signs the test client in as the admin. The admin page now answers 200.
client.login(username="sana", password="secret123")
response = client.get("/admin/")
print(response.status_code)
print("Site administration" in response.content.decode())Example
import django
from django.conf import settings
settings.configure(
DEBUG=True,
SECRET_KEY="key",
ROOT_URLCONF=__name__,
ALLOWED_HOSTS=["*"],
INSTALLED_APPS=[
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
],
MIDDLEWARE=[
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
],
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
TEMPLATES=[{"BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [], "APP_DIRS": True, "OPTIONS": {"context_processors": ["django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages"]}}],
STATIC_URL="/static/",
)
django.setup()
from django.contrib import admin
from django.contrib.auth.models import User
from django.core.management import call_command
from django.urls import path
urlpatterns = [path("admin/", admin.site.urls)]
call_command("migrate", verbosity=0)
User.objects.create_superuser("sana", "sana@example.com", "secret123")
from django.test import Client
client = Client()
print(client.get("/admin/").status_code)
client.login(username="sana", password="secret123")
response = client.get("/admin/")
print(response.status_code)
print("Site administration" in response.content.decode())
Key points
- The admin site ships with Django, so no extra install is needed.
- The admin lives at /admin/ in the project.
- Use createsuperuser to make an admin account.
- The admin account has a name and a password.
- Log in at /admin/ with the admin name and password.
In this example, the setup lines configure Django. One more setup line names the address for static files. Static files get their own lesson later. The setup also adds the admin apps and the middleware list. Migrate builds the tables for the admin and the user model. The createsuperuser line makes the admin account named sana. The test client asks for /admin/ without logging in. The admin answers 302, a redirect to the login page. Login signs the client in, and the next ask answers 200. The last print checks that the page carries the words Site administration.
Note
A plain user account cannot log in to the admin.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does createsuperuser make?