How to use the Django create superuser command

When it comes to managing the site you’re developing, Django includes a powerful admin interface that lets you interact with your site’s data more easily. It’s a built-in app that generates GUI for performing CRUD operations over your site’s data. Admin panel is one of the most useful Django features that differentiate it from other web frameworks. By default, it is already included over django.contrib.admin module in the INSTALLED_APPS.

To access the Admin interface, you must have at least one superuser. You can easily create a superuser in Django by typing the following command.

$ python manage.py createsuperuser

Let’s first see what a superuser is and how to create one using different methods. In the end, we’ll show how the newly created superuser can access the admin interface.

What is a Django superuser?

A superuser is an admin user in Django with some extra parameters enabling it to have greater rights than the ordinary user. More precisely, the superuser has all permissions enabled by default. Be careful who will be able to control the superuser since it can modify (create, update, delete) all data. 

If you want users with a more limited scope, you can skip creating a superuser but create a staff user instead. The staff user will also be able to log in to an admin panel, but specific permissions must be set up before accessing any resources.

Create superuser in Django – using the command line

Before creating a superuser, ensure you have executed the migrate command since the admin interface is part of a django.contrib.admin module and superuser is using authentication mechanisms from the django.contrib.auth module.

$ python manage.py migrate

Now, to create a superuser, type the createsuperuser command.

$ python manage.py createsuperuser

Django will prompt you to enter the details such as username, email address, and password.

Username (leave blank to use 'admin'): testuser
Email address: testuser@example.com
Password: ********
Password (again): ********
Superuser created successfully.

To create a Django superuser without prompt jumping out, you can define all the necessary data as environment variables and then use the createsuperuser --noinput command. This is perfect for a scenario when you want to automate the superuser creation and avoid prompts.

$ export DJANGO_SUPERUSER_USERNAME=testuser
$ export DJANGO_SUPERUSER_PASSWORD=testpass
$ export DJANGO_SUPERUSER_EMAIL="testuser@example.com"
$ python manage.py createsuperuser --noinput

Voila, your superuser is created and ready to rule!

Create superuser in Django – programmatically

Another way of creating a desired superuser can be directly through code. I’ll show you how to create a superuser inside Django’s shell. Again, to make it possible, make sure you have migrated your database first.

You can now start the shell by typing the following command.

$ python manage.py shell

Then type the following snippet to create a superuser.

>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()
>>> User.objects.create_superuser('testuser', 'testuser@example.com', 'testpass')
<User: testuser>
>>> exit()

In the above snippet, User model is first imported from the preinstalled authentication system. Then a superuser is created using the create_superuser() method, which takes three arguments: username, email, and password

You can also use it as a one-liner, which can also be used for automating superuser creation since it executes without a prompt.

$ echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('testuser', 'testuser@example.com', 'testpass')" | python manage.py shell

Accessing the admin interface

To test if the superuser was successfully created, you can simply try to access the admin interface in Django. First, you need to run the development server by typing:

$ python manage.py runserver

If you navigate to http://127.0.0.1:8000/admin/, a login screen for the admin panel will appear. Enter the credentials you previously used for creating a superuser and hit the Log In button.

Django's admin panel, screen where newly created superuser can log in.

The admin interface is located by default on http://127.0.0.1:8000/admin/, but if you want, you can change the URL in the urls.py.

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls), # here
]
NOTE - Django’s admin panel should not be used as an alternative for regular users to modify the site’s content.

If you started a project from scratch, you should see Groups and Users models inside the home dashboard. Otherwise, you should see all other models you registered for the admin panel.

Django's admin panel, screen where newly created superuser can see registered models.