A Django superuser is an essential administrative account with elevated privileges within a Django project. With the ability to perform administrative tasks, a superuser holds complete control over the project’s data and functionality. This powerful account can access the Django admin interface, which allows it to manage various aspects of the application. From adding, editing, and deleting data to managing user accounts and configuring application settings, a superuser is equipped with comprehensive administrative capabilities.
We can perform the following steps to create a superuser in a Django application:
Step 1: Run the following command:
python3 manage.py createsuperuser
Step 2: Type in a desired username for the superuser.
Step 3: Provide an email address for the superuser. This step is optional, so we can leave it blank by pressing “Enter.”
Step 4: Enter and confirm a password for the superuser.
Note: The password will not be visible to us as we type it in.
Let’s create a superuser in a simple Django application in the code widget below. We’ll use the following steps:
Click the “Run” button to start the application.
Once the server starts and the Django admin page opens in the “Output” tab, click the “+” button to open a new terminal.
Execute the command cd usercode
to go into the project directory.
Follow the steps given above to create the superuser.
Once superuser creation is successful, try logging in to the Django admin panel (shown in the “Output” tab) as a superuser using the credentials you just set up.
echo "from django.contrib.auth.models import User; User.objects.create_superuser('johndoe', 'john@example.com', 'john123')" | python3 manage.py shell
createsuperuser
Creating a Django superuser automatically or non-interactively can be useful in various scenarios and development workflows for the following reasons:
Development and testing: During the development and testing phases, it is common to reset the database or start with a clean state. Automating the creation of a superuser allows us to easily set up an initial administrative account without manual intervention. This is particularly helpful when setting up development environments on multiple machines or when sharing the project with other developers.
Continuous integration and deployment: In a continuous integration (CI) or deployment pipeline, automatically creating a superuser can be part of the initialization or setup process. It ensures that the necessary administrative account is available for performing administrative tasks in the deployed environment.
Docker and containerized environments: When using containerization technologies like Docker, we can automate the creation of a superuser during the container initialization or provisioning process. This enables seamless deployment of containers with an already configured superuser.
createsuperuser
We can either automate createsuperuser
using the automation command or via shell script.
To automate the creation of a superuser, we can use the following command:
echo "from django.contrib.auth.models import User; User.objects.create_superuser('<username>', '<email>', '<password>')" | python3 manage.py shell
While creating a superuser using the above command, replace <username>
, <email>
, and <password>
with the username, email and password of the superuser, respectively. Let’s try to create a superuser automatically in the above widget using the following command:
echo "from django.contrib.auth.models import User; User.objects.create_superuser('educative', 'edu@example.com', 'edu123')" | python3 manage.py shell
To create a superuser automatically using a shell script, we can simply put the above command in the script file and execute that script file. To practice this, we can use the script.sh
file in the above widget. Executing that file in the terminal using ./script.sh
will create a superuser with username johndoe
and password john123
.
Free Resources