The Admin Site
Part of the Models and the Admin section of Coddy's Django journey. Lesson 20 of 23.
Whoever runs the bookshelf needs to add and fix books without writing code. Django comes with an admin for that: a site where staff add, change and delete the rows of any model. It is already on: mysite/urls.py sends admin/ to admin.site.urls.
To log in you need a superuser, an account that may do everything. On your own computer you run python manage.py createsuperuser; here, type createsuperuser in the Shell tab. Every Run starts with an empty database, so create it again after each Run.
The admin shows only the models you register in the app's admin.py:
from django.contrib import admin
from .models import Loan
admin.site.register(Loan)Each registered model gets a list page and forms built from its fields, with a drop-down for choices and a menu of rows for a ForeignKey. Each row is named by its __str__.
Challenge
Easy- In
books/admin.py, registerBook,AuthorandTag.
Press Run, type createsuperuser in the Shell tab, and log in at /admin/. Add a book there, then find it on the home page.
Try it yourself
from django.contrib import admin
# Register your models here.
All lessons in Models and the Admin
Practice on your own: Django playground