How to Create a Superuser in Django?

Learn via video courses
Topics Covered

Django simplifies administration tasks by offering an integrated Admin panel, eliminating the need to create a separate Admin page or implement authentication features. However, it's crucial to migrate the project beforehand to ensure the creation of the superuser database. Without this migration, the Admin panel functionality won't be fully realized.

How to Create a Superuser in Django?

Django provides a default admin interface that is required to perform operations like create, read, update, and delete on the model directly. It reads a set of data that gives information about data from the model, to provide an instant interface where the user can handle the contents of the application. To access the admin interface, Django creates a superuser command is used.

The admin app is activated by default and already added into the INSTALLED_APPS present in the settings.py file. 'localhost:8000/admin/' is used to access the admin interface in the browser. Before creating a superuser we are first to enter the following commands in the command prompt to make migrations and then to migrate.

After running the above two commands we are to create a superuser using the command given below:

The user is then asked to create a username, then to enter an email address, and the password. When entering the password, we will notice that no words are visible for security purposes. After creating the Django superuser, we start the server using the command:

Open the URL 'localhost:8000/admin/' in the browser again, enter your username and password then login. A Django Admin Dashboard opens where we can add, remove and update data belonging to any registered model.

Django’s “Hello World” Application

In the views.py file,

In the above code,

  • At first, we imported HttpResponse from Django.http`module.
  • We created a function index under request.
  • The function first takes a request and returns the HTTP object 'Hello World!!!!'.

In the urls.py file, we are supposed to link the function we created in the views.py file under the curl pattern and also import the view function in the beginning.

In the above code,

  • We first import the views from the application.
  • Under URL pattern we created a path under which we leave the URL host part blank with an empty '' which will by default be set to 8000/, however, if we wish to we can add the host, next we linked the function first, and lastly, we named it 'home'.

We go to the command prompt and enter the following command.

Now we open the url http://127.0.0.1:8000/ in the browser. We see a webpage will open with the message 'Hello World!!!!', as we specified in the views.py file earlier. If in the urls.py file we linked our view function and entered some other host then we were supposed to open the url http://127.0.0.1:_____/ the blanks after the colon should have contained the host mentioned instead.

Conclusion

Hello developer!! I am sure by now you must have learned about the Django superuser and also how to create it. Let us summarize what we have learned so far

  • Django is a high-level Python web framework that allows for the quick creation of secure and maintainable websites.
  • Django is a collection of Python libraries that allows us to create useful web apps for both the backend and front ends.
  • Django provides a default admin interface that is required to perform operations like create, read, update, and delete on the model directly.
  • To access the admin interface, Django creates a superuser command is used.
  • 'localhost:8000/admin/' is used to access the admin interface in the browser.