Django Create Superuser
For managing the website, we need an Admin while creating the Django application. In order to create the admin user, create superuser command is used. The superuser is called the admin user. Django provides an inbuilt Admin Panel along with the authentication, which is not to be created separately. Migrating the Django project is necessary before creating a superuser database. There are three types of users:
- Regular User– No Admin Access
- Staff User – Restricted Admin Access
- SuperUser – Full Access
Options of Superuser
Django createsuperuser has various options that may be needed while customizing, like automating superuser creation or using different databases.
manage.py createsuperuser [-h] [--username USERNAME] [--noinput] [--database DATABASE] [--email EMAIL] [--version] [-v{0,1,2,3}] [--settings SETTINGS][--pythonpath PYTHONPATH] [--traceback] [--no color] [--force-color] [--skip-checks]
Option | Description |
-h, --help | Show help message |
--username USERNAME | Specify login name for the superuser |
--noinput, --no-input | Django will NOT prompt the user for any kind of input. Superuser will not be able to log in until given a valid password if it is created with this option. It can be used to automate superuser creation |
--database DATABASE | Specify the database to use. By default, it is "default." |
--email EMAIL | Specify email for the superuser |
--version | Show program version and exit |
-v{0,1,2,3}, --verbosity{0,1,2,3} | Verbosity level 0 = Minimal Output 1 = Normal Output 2 = Verbose Output 3 = Very Verbose Output |
--settings SETTINGS | Python path to the settings module. By default, the DJANGO_SETTINGS_MODULE environment variable is used |
--pythonpath PYTHONPATH | Directory to add to the python path |
--traceback | Raised on CommandError exception. |
--no-color | Do not colorize command output |
--force-color | Force colorization of command output |
--skip-checks | Skip system check |
--verbosity and --traceback can be used to diagnose errors with createsuperuser.
--database, --settings, --pythonpath allows you to run the command with custom databases and settings.
--no-color, --force-color can be beneficial for people with color blindness or messed terminals
Create Superuser
Step 1:
Run the following command
(cms) D:\Django\cms\djangocms>python manage.py createsuperuser
Step 2:
Enter username. By default, it is the system username.
Username (leave blank to use 'nikhil '): user1
Step 3:
Enter the email address.
Email address: [email protected]
Step 4:
Enter the password, and after pressing enter, you will be asked to enter the password again for confirmation.
Password:
Password (again):
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
If the password entered is common, you will be asked to bypass password validation. If you still want to use the common password, enter y and press enter else, press N, and create another not common password. After entering the password, the superuser is created successfully.
Step 5:
Now, login to the Admin page by running the following command:
(cms) D:\Django\cms\djangocms>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 27, 2021 - 12:21:51
Django version 3.2.5, using settings 'djangocms.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
In the browser, go to "/admin/" on the local server http://127.0.0.1:8000/ or "http://127.0.0.1:8000/admin/". The following page will appear.
Now enter the Username and Password set while creating the superuser.
The admin Page after a successful login will look like this:
Now you can create or change groups and users, which is provided by django.contrib.auth the authentication framework.