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 log in to the Django project admin website and how to add auth users with it. The auth user is a built-in Django application that provides user authentication and authorization.
1. Create Django Admin Site SuperUser.
- Django admin site should have one superuser.
- So after you create the Django project, you should create the superuser for it first, then you can use this superuser to login to the admin site.
- Follow the below steps to create a Django project admin site superuser.
- Open a terminal and go to the Django project folder.
- Then execute
python3 manage.py createsuperuserto create the superuser, the process is interactive, you need to provide the superuser’s 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 log in to it.
urlpatterns = [ path('admin/', admin.site.urls), ...... ]
2. Change Admin Site SuperUser Password.
- If you forget the admin superuser password, you can execute the below command to change it.
python3 manage.py changepassword admin
- If you even forget the superuser name, you can execute the command
python3 manage.py createsuperuseragain 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 migratescript to save the changes to the SQLite database.
3. Add Staff User In Django Admin Site.
- After you log in to the Django admin site, you can add auth user and group by clicking the Add link at the end of the 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 models are mapped to related database tables, the default database is SQLite.
- User model map to the DB table auth_user, Group model map to the 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 the Add link after the User model.
- Input username, password then click the Save button.
- But you need to edit the added user data again, to check the Staff status checkbox.
- If you do not check the Staff status checkbox you may encounter an 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 log in to the admin site.
- Now log out and use the new user account to login again successfully.