Fortunately, we do not need to do much work to create an admin site for our django project, because admin
is a built-in app of Django and we can use it directly.
let' go to our django
package and have a look at the structure:
├── django
│ ├── apps
│ ├── bin
│ ├── conf
│ └── contrib
│ ├── admin
we can see the built-in admin
app located in django > contrib
add admin into INSTALLED_APPS
in settings.py
:
INSTALLED_APPS = [
...
'django.contrib.admin',
]
also notice some packages are also needed:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.messages",
]
to include urls of admin, we should import admin first:
from django.contirb import admin
urlpatterns = [
...
path('admin/', admin.site.urls), # note: no quotation mark
...
]
before we can create a superuser, we need to make migrations for admin app first, or you will get an error like:
You have 3 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin.
Run 'python manage.py migrate' to apply them.
so, let's migrate first:
python3 manage.py migrate admin
# results:
Running migrations:
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
go to the root directory of our django project (where manage.py
located) and run:
python3 manage.py createsuperuser
# fill in some information
Username:
Email address:
Password:
Password (again):
now, you can access your admin site through http://yourdomain.com/admin/