How to Create a Superuser in Django?
Learn via video courses
Overview
Django is a high-level Python web framework that allows for the quick creation of secure and maintainable websites. It is open source and free. It is used for full-stack application development as well as server development. It is a collection of Python libraries that allows us to create useful web apps for both the backend and front ends.
Scope
In this article,
- We will learn about Django create superuser command.
- We will also learn about Django superuser.
- We will also learn about Django's “Hello World” application.
Introduction
Django is a high-level Python web framework used for full-stack app development and server development. With the features like a login system, database connection, and CRUD operation, Django makes the creation of an app easier. Let us create a project in Django named World of Glitten and an application named Hello World.
We activate a virtual environment at first then start with the creation of the project.
In the steps above after activating the virtual environment, we created a project using the command Django-admin start a project, and then we moved into the project directory by using the command cd followed by the name of the project. Using python manage.py startup we created the application HelloWorld.
When we open the directory of the project from our system's drive we see the following files and folder.
- HelloWorld:
It is the folder of the application we created. - World_of_Glitten:
It is the inner project directory containing some files and folders required by the developer. - manage.py:
It is a file with the extention '.py'.
Inside the inner project directory is the World_of_Glitten directory we find the following:
- pycache
- init.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
In the settings.py file, under the INSTALLED_APPS section, we write the name of the application we created.
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.