The one Django feature that makes it stand out against other web development frameworks like Laravel and Ruby on Rails is its powerful Admin interface which provides a visual means of interacting with data. Inside the Django Admin interface, you can create, read, update, and delete your database records very easily and faster. To learn more about this Django feature, check out The Django Admin Site documentation from Django’s official site.
In order to access and use the Django administration panel, however, you must first set up a superuser who can log in. If you’re a Django newbie or even a professional, this step-by-step post will show you 2 methods to create a superuser in Django.
I’ll start by explaining what a Django superuser is, then I’ll state some steps you should take before creating one. After that, we will get into the 2 methods to create a Django superuser. Then some recommendations on which method you should use and when and finally some frequently asked questions on the matter. I’ll also be getting into the theory behind it so that you can understand how things work. Hopefully, after you read this, you’ll be equipped with enough knowledge to create a Django superuser on your own.
So let’s get started.
Table of Contents
- What Is A Django Superuser?
- 1. Create Django SuperUser With the createsuperuser command.
- 2. How To Create Django User From The Django Shell
- Frequently Asked Questions on How To Create A Django SuperUser.
What Is A Django Superuser?
If you’re new to Django, or to back-end web development in general, you might think that the term ‘superuser’ is something that Django came up with from scratch because of the popular
createsuperuser command. However, that’s not the case. In the computing world, a superuser is simply an account with access significantly above those of the typical user. Perhaps the term’s synonym that’s more popular is ‘administrator’ or in short ‘admin’.
Therefore, In Django, a superuser is a special type of user who has access to the admin panel and has all the permissions available.
Now let’s see how to create a Django superuser.
1. Create Django SuperUser With the
The easiest and most popular way to create a superuser in Django is to use the
createsuperuser command. You simply have to type the command:
python manage.py createsuperuser in your terminal and you’ll be prompted to enter first your username, then your email, and finally your password. Then you can use those details to login into the admin interface.
Here is how to do it.
createsuperuser command uses Django’s authentication system which comes preinstalled in the latest versions of Django. You can see this in the list of installed apps in the
settings.py file of your Django project folder. That’s the
django.contrib.auth on line 5 of the snippet code below.
# project folder/settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # right here 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
This means that you don’t really have to care about the installation of Django’s authentication system. Django has already done that for you. However, before you create a superuser, you must migrate your database so that these installed apps can be recorded in the database.
Run the following command in your terminal to migrate.
$ python manage.py migrate
Good. Now you have to create a superuser. Run the following command in your terminal to create a Django superuser. Make sure you remember the details you provide here for you are going to use them to log into the admin panel.
$ python manage.py createsuperuser Username (leave blank to use 'computername'): Email: Password: Password (again): Superuser created successfully.
Note: The command line will not show you your password as you type it for security reasons. The Email is not mandatory, if you do not want to specify it, you can just press Enter instead of the email itself.
If the command line prints ‘Superuser created successfully’ after you enter your details then we can now login into the admin.
If you look in the
urls.py file of your project folder. You’ll see that the
admin/ URL is pointing to the admin panel. This means that it is the URL we have to enter on our address bar.
# project folder/urls.py from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), # here ]
But first. You have to run the Django server.
Run the following in your terminal to run the Django development server.
$ python manage.py runserver
Now that the server is running, in your browser, navigate to http://127.0.0.1:8000/admin/. Do you see the
admin/ at the end we have been talking about?
You now see this page.
Type the username and password you’ve created before and hit the Log in button. And You’re In! To learn more, check out how to access the admin panel.
2. How To Create Django User From The Django Shell
The Django shell is an alternative way of accessing the database interactively. Meaning a line of code is run at the moment of its creation one by one.
Let’s see how we can create a Django superuser using the Django shell.
➊$ python manage.py shell ➋>>> from django.contrib.auth import get_user_model >>> User = get_user_model() ➌>>> User.objects.create_superuser('username', 'email', 'password') <User: username> ➍>>> exit()
Let’s see what the code above is doing.
python manage.py shell is a command that we use to activate the Django shell. Once the shell is activated, this is shown by
>>> at the beginning of each line we create.
➋We import the User model from Django’s authentication system.
➌We create a superuser with the
create_superuser function which takes in 3 arguments namely, username which is your username, email, and password. The email is not required for the superuser to be created. If you decide to leave out the email, you have to specify the password’s argument. That is, you’ll have to type
password = 'yourPassword'
After this, the user will be successfully created and this is shown by its username being printed out on the console.
exit() takes you out of the Django shell. After then, you can run the server and log in to the admin panel using the details you gave.
You can also do this in one line:
$ echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('username', 'email', 'password')" | python manage.py shell
Older versions of this same code would be:
$ python manage.py shell >>> from django.contrib.auth import User >>> User.objects.create_superuser('username', 'email', 'password') <User: username> >>> exit()
Or the one-liner:
$ echo "from django.contrib.auth.models import User; User.objects.create_superuser('username', 'email', 'password')" | python manage.py shell
Frequently Asked Questions on How To Create A Django SuperUser.
Which command is used to create a superuser in Django?
The command that is used to create a superuser in Django is
python manage.py createsuperuser. Before you run this command though, make sure you have migrated your database by running
python manage.py migrate. This is a one-time command, you don’t have to run it every time before you create a superuser, unless you’ve changed your settings.py or you’ve made migrations.
Conclusion: How To Create A Superuser In Django.
Congrats you’ve moved this far. I’m so glad I was able to help. Let me know in the comments section below what you think about this post. Share it with fellow developers whom you think may need it.
If you could not follow along with this tutorial, that is you were not able to create a Django superuser using the given information, once again, let me know in the comments section. And if you have other methods of creating a Django superuser that you know that I did not mention in this post, feel free to share them in the comments section below. If genuine, they will definitely become part of this list.
Anyway, see you in other codingGear guides. Peace!