How to install Django on Ubuntu 20.04?

Overview

Django is a web framework for Python that facilitates fast application development. Its design aims to enable developers to take applications from the beginning to the end as swiftly as possible. Django offers robust security features to help prevent developers from making common security errors, ensuring that their applications are more secure. In this guide, we will walk you through the step-by-step process of installing Django on an Ubuntu 20.04 server. Additionally, this will help you create a sample Django application. Let's begin!

Prerequisites

There are certain prerequisites that need to be met before you begin:

  • Server running Ubuntu 20.04

  • Access to SSH-connected text editor

  • User account with root or sudo access

  • Internet connection

Get Started

Step 1: Updating the Linux System

  • Enter the root user, with the following command:

sudo -i 
  • Now, update the Linux system, using the command below:

sudo apt update -y 

Step 2: Installing Python and pip

  • Most of the recent operating systems have Python 3 pre-installed by default. However, if Python is not already installed on your system, you can use the following commands to install Python3 and pip:

 sudo apt install python3 python3-pip -y  

  • Check the current version of installed Python, with the following command:

python3 -V 

  • Check the current version of the installed pip, by running the following command:

pip3 -V 

Step 3: Installing Django using PIP

  • There are two ways to install Django, either by using the source code from the Github repository or by using PIP. In this tutorial, we'll install Django using PIP, by running the following command in the terminal:

pip3 install Django 

  • Run the following command to check the current version of the installed Django:

django-admin -–version 

Step 4: Creating Django Application

  • To create a new Django application, you must first navigate to the directory where you want to locate it. Once you are in the correct directory, execute the following command followed by the desired project name:

mkdir -p /var/www && cd /var/www 
django-admin startproject <name-of-your-django_project> 

  • Enter your the Django_project, with the following command:

cd django_app 
  • Now, you must migrate the final dependencies of your Django project, which basically is creating a database for your application:

python3 manage.py migrate 

Step 5: Creating Django Super Admin Account

  • It is necessary to create a superuser account to manage the Django application. To accomplish this, execute the command provided below from within the directory of the Django application:

python3 manage.py createsuperuser 

Note:  Your superuser password must contain at least 8 alphanumeric characters with a mix of uppercase and lowercase alphabets, to avoid the errors as shown below:

Step 6: Running the Django Application

  • The Django application is now ready to work upon. By default, external access to the web interface is restricted by Django. For allowing external hosts, you must modify the settings.py file and add the desired IP address to the ALLOWED_HOSTS section.

vi django_app/settings.py 
  • Add your IP address, where your Django application is installed, in the following way:

ALLOWED_HOSTS = [‘your-IP-address-here']  

  • To launch the Django application server, execute the command provided below.

Note: The IP address "0.0.0.0:9000" indicates that Django will listen on all available interfaces on port 9000. If you wish to use a different port, feel free to modify it accordingly.

python3 manage.py runserver 0.0.0.0:9000 

  • The Django application server is currently running. To view the default Django web page, open your preferred web browser and access the IP address of the Django system on port 9000.

http://<your-IP-address>:9000 

  • In addition, you can gain access to the Django administrator page by accessing the /admin subdirectory URL. To log in, use the superuser credentials that you created in the previous step. An example of the URL for the administrator page is

http://<your-IP-address>:9000/admin 

  • Upon accessing the Django admin dashboard, you will be presented with a user interface similar to the one depicted below. From here, you can manage and add users and groups to your application as needed.

Conclusion

You have completed the process of creating a Django application on your system. Now you can begin building your application using Django.

Last updated