Introduction to the Django Admin
What the Admin App Gives You
Django ships with django.contrib.admin already listed in INSTALLED_APPS and already routed in urls.py. It reads your models and builds list, add, edit, and delete pages for them automatically -- no HTML required from you.
Note: You can see it in any fresh Django project by visiting /admin/ after creating a superuser.
Example: What the Admin App Gives You
This is the line already present in a new project's urls.py that wires the admin app to the /admin/ path.
urlpatterns = [
path('admin/', admin.site.urls),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Logging In
The admin is protected by a login screen. Only a user marked as staff (and usually a superuser) can sign in, so you need an account with those permissions before you can use it.
Example: Logging In
Running this command creates the account you'll use to log in to /admin/.
python manage.py createsuperuser
⚠️ Run this command in your terminal.
Why Models Don't Appear by Default
Django doesn't guess which models you want managed in the admin. Each model must be registered in its app's admin.py, or the admin site simply won't list it.
Warning: A model with no admin.py registration is invisible in the dashboard even though it exists in the database.
Example: Why Models Don't Appear by Default
Adding this to an app's admin.py makes the Book model show up in the admin dashboard.
from django.contrib import admin
from .models import Book
admin.site.register(Book)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Assuming the admin site is meant to be the public-facing part of a website instead of a private management tool.
- Forgetting that the admin only shows models you've explicitly registered, then wondering why a model doesnt appear'.
- Leaving the default /admin/ URL and default superuser credentials exposed on a real production deployment.
- django.contrib.admin is a built-in app that auto-generates a full CRUD interface for your models.
- It's enabled by default in a new project's INSTALLED_APPS and wired up in urls.py.
- You still need to register each model before it appears in the admin.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: