Each Django project has an admin website. You can use this website to manage Django project auth user, user group and manage custom Django application’s models etc. This article will tell you how to login to Django project admin website and how to add auth user with it. The auth user is a built-in Django application which provide user authentication and authorization.
1. Create Django Admin Site SuperUser.
Django admin site should have one superuser. So after you create Django project, you should create the superuser for it first, then you can use this superuser to login to the admin site.
Follow below steps to create Django project admin site superuser.
- Open terminal and go to Django project folder.
- Then execute
python3 manage.py createsuperuser
to create the super user, the process is interactive, you need to provide superuser name, email and password in the process.> python3 manage.py createsuperuser bash -cl "/usr/bin/python3.6 /home/zhaosong/WorkSpace/Tool/pycharm-professional/pycharm-professional-2018.3.3/pycharm-2018.3.3/helpers/pycharm/django_manage.py createsuperuser /home/zhaosong/WorkSpace/Work/dev2qa.com-example-code/PythonPyCharmPoject/DjangoHelloWorld" File tracking disabled System check identified some issues: WARNINGS: ?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs. Username (leave blank to use 'zhaosong'): admin Email address: [email protected] Warning: Password input may be echoed. Password: ******** Warning: Password input may be echoed. Password (again): ******** Superuser created successfully. Process finished with exit code 0
- Now you can browse the Django admin site with url http://127.0.0.1:8000/admin/. This url is defined in DjangoHelloWorld / DjangoHelloWorld / urls.py file as below. Then you can use the superuser name and password to login to it.
urlpatterns = [ path('admin/', admin.site.urls), ...... ]
2. Change Admin Site SuperUser Password.
If you forget the admin superuser password, you can execute below command to change it.
python3 manage.py changepassword admin
If you even forget the supperuser name, you can execute python3 manage.py createsuperuser
script again to create another admin site superuser and use it.
If you really want to use the old admin user name as the superuser name, you can delete the DjangoHelloWorld / db.sqlite3 file and then recreate the same superuser again. But after that, you may need to run python3 manage.py migrate
script to save the changes to sqlite database.
3. Add Staff User In Django Admin Site.
After you login to Django admin site, you can add auth user and group by click the Add link at the end of Users or Groups model.
The User model full class package path is django.contrib.auth.models.User and the Group model full class package path is django.contrib.auth.models.Group.
All these model mapped to related database tables, the default database is sqlite. User model map to db table auth_user, Group model map to db table auth_group. When you add a user, user data will be inserted in table auth_user.
3.1 Add Auth User In Admin Site Steps.
- Click Add link after Users model.
- Input username, password then click Save button.
- But you need to edit the added user data again, to check the Staff status checkbox. If you do not check Staff status checkbox you may encounter error message like Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive. when you use the newly added user to login to the admin site.
- Now logout and use the new user account to login again successfully.