How To Use The Django Admin Site To Create Auth User

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.

  1. Django admin site should have one superuser.
  2. 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.
  3. Follow the below steps to create a Django project admin site superuser.
  4. Open a terminal and go to the Django project folder.
  5. Then execute python3 manage.py createsuperuser to 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
  6. 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.

  1. If you forget the admin superuser password, you can execute the below command to change it.
    python3 manage.py changepassword admin
  2. If you even forget the superuser name, you can execute the command python3 manage.py createsuperuser again to create another admin site superuser and use it.
  3. 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.
  4. But after that, you may need to run python3 manage.py migrate script to save the changes to the SQLite database.

3. Add Staff User In Django Admin Site.

  1. 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.
  2. 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.
  3. All these models are mapped to related database tables, the default database is SQLite.
  4. User model map to the DB table auth_user, Group model map to the DB table auth_group.
  5. When you add a user, user data will be inserted in table auth_user.

3.1 Add Auth User In Admin Site Steps.

  1. Click the Add link after the User model.
  2. Input username, password then click the Save button.
  3. But you need to edit the added user data again, to check the Staff status checkbox.
  4. 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.
  5. Note that both fields may be case-sensitive when you use the newly added user to log in to the admin site.
  6. Now log out and use the new user account to login again successfully.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.