Ubuntu

How to Install Django on Ubuntu 20.04

How to Install Django on Ubuntu 20.04

Introduction:

Django is an open Python-based web app approach that helps customers speed up the app development procedure by eliminating common mistakes like SQL Injection, CSRF, and XXS. It uses the Model-View-Controller (MVC) paradigm, making it a compact and reliable development tool. Application developers from all around the globe, notably large IT companies, utilize it. It has also been used to build some of the most well-known websites online. Instagram, Pinterest, and Knight Foundation are among the utmost well-known websites built with Django.

Methods to install Django:

Following are the two basic methods for the installation of Django on the Ubuntu 20.04 system.

Install Django Via Apt Package:

Make sure to have python’s newest version installed on your Linux system otherwise we cannot work with Django. We have the python3 version installed on our Ubuntu 20.04 system. So, open the Ubuntu 20.04 system and log in from it. Use the “Ctrl+Alt+T” shortcut to open the terminal shell because we have to install it via console. After you have opened the shell, make sure to update your apt package via the below query:

$ sudo apt update

After the update, we will be installing Django using the apt command and “python3” keyword. Hence, execute the listed below instruction in your shell to install Django using sudo privileges:

$ sudo apt install python3-Django

It requires your current user password to continue. Add your password and press “Enter”. It will start installing Django via python3.

Meanwhile, between the installation, it requires your affirmation to install Django by asking “Do you want to continue? [Y/n]?”. You have to press “y” to proceed with the installation of Django. If you want to quit it press “n”.

The installation will get completed after your get these last process line.

You can see the installed version of Django on your Ubuntu 20.04 system by using the below query:

$ django-admin --version

Another way to check and import Django on your system is via python. Firstly convert your shell into a python environment shell using the below python3 keyword.

$ python3

After that, you have to write python code to import and check the installed version of Django. For importing Django use the import keyword with its name. In the other line, use the print statement to show the latest installed version of Django by getting it via the get_version() method of the Django package.

>>> import django
>>> print(django.get_version())

Install Django via Pip Repository:

Another easier way to install Django on your system is via the “pip” repository. Hence, open the shell, and try out the below-stated query in it followed by the version of Django. It will start installing the mentioned version of Django on your system with the “pip” repository as you can see the process in the snapshot.

$ pip install django==3.0.0

Make Django Application:

To begin, go to the folder where you want to construct a new app. Use the “mkdir” command to create a new directory. As you can see we have been making an app “new” in the home directory of our system. It requires our current account password. Hence, we have added it and proceed.

$ sudo mkdir –p /home/project/new 

Furthermore, to establish a fresh Django app, just use “Django-admin” and start project instruction along with the name of an application which is “proj1” in our case. In your machine, launch a console and add:

$ django-admin startproject proj1

After this navigate to the newly created app “proj1” using cd query as:

$ cd proj1

After this, we have to migrate the changes that are being pending via the below python3 query migrating the file “manage.py”. Now the system will migrate all the required pending changes.

$ python3 manage.py migrate

Make Super User:

Furthermore, for Django application management, build a standard user profile. Within the Django application folder, execute the stated below createsuperuser instruction.

$ python3 manage.py createsuperuser

It will ask you to add a new username. If you want to continue with the already logged-in user leave it empty. Provide email address and password twice. As you can see in the snapshot that it will affirm our action by asking [Y/n]. Press Y to complete the creation of the superuser.

Remove Django:

To uninstall the installed Django from your Linux system, try out the below pip uninstall instruction in the shell.

$ sudo pip uninstall django

Proceed with the uninstallation of Django by tapping “y”.

Conclusion:

This article covers the two methods to install Django on Ubuntu 20.04. Furthermore, it also elaborates to create Django applications, superuser creation, and removal of Django.

Similar Posts