What is Django
Django is an open-source framework written in Python and used to develop web applications based on Python. A framework is a collection of built-in functions and modules used to make development easier.
Why do we use it?
It can provide functionality like authentication support, management and admin panels, contact forms, comment boxes, file upload support, and more. In other words, if you were creating a website from scratch, you would need to develop these components yourself. However, if you are using framework instead, these components are already built, you just need to configure them properly to match your site.
Django offers many modules you can use in your projects or website development so today I will be showing you how to install Django on Ubuntu Linux operating systems.
So now we know what is Django and why is it used let’s start installing it. Now, as Django is a Python framework you must have Python installed on your machine. For installing Python simply type this in your terminal
$ sudo apt-get install python
Since Django 2.2.2 (latest version) now requires Python 3 to be installed so for this just type
$ sudo apt-get install python3
To check the version of the Python installed on your system type
$ python --version
$ python -V
To check the version, python 3 just type
$ python3 --version
To install Django you need to install ‘pip’ so you can easily download Django and install otherwise you have to download it from the site which is here.
What is a pip? Pip is the standard package manager for Python. It allows you to install and manage additional packages that are not part of the Python standard library. You can install pip by typing this in your terminal
$ sudo apt-get install python3-pip
After this, we need to install the Virtual Environment.
What is a Virtual Environment?
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools which most Python developers use.
Why do we need a virtual environment?
Imagine a scenario where you are working on two web-based python projects and one of them uses Django 1.9 and the other uses Django 2.2.2 and so on. In such situations, virtual environments can be really helpful to maintain the dependencies of both projects.
The great thing about it is that there are no limits to the number of environments you can have since they’re just folders containing a few scripts.
We use a module ‘virtualenv’. This tool allows us to create isolated Python environments. Virtualenv creates a folder that contains all the necessary executable files to use the packages that a Python project would need.
Installing Virtual Environment
To install the virtual environment just type this in the terminal
$ sudo apt-get install python3-venv
$ pip install virtualenv
Installation of the virtual environment is complete. Now let’s create a directory where we will create a virtual environment. Type this in the terminal to create a folder named project.
$ mkdir project
and then change directory to project
$ cd project
Now we will create the virtual environment here and for that, we will type
$ python3 -m venv env
It will create a folder named env inside the folder ‘project’ that we have just created like this
And as you can see there are some files inside this folder ‘env’
Activate the environment
Now that we have created the environment, After that, we need to activate the environment so that whenever we execute python or any command it will be loaded from this folder, for example, to see from which folder python is being loaded into memory you have to type
$ which python3
As we can see that its path is /usr/bin/python for now.
Now we will activate the environment and for this, you need to type this in the terminal,
$ source env/bin/activate
Now python is loading from a new environment which is
/home/tabinda/project/env/bin/python3. It means the virtual environment is now active.
Now let’s install Django and execute this command in the terminal
$ pip install django
Installation of Django is complete in the new environment. It is time to create a new project heyworld like this
$ django-admin startproject heyworld
At this stage, Django is installed properly in the environment and we will run this project.
Also Read: How to Use 7Zip in Linux [Quick Tip]
Django has its own built-in web server which can be used for development purposes and we will use it to execute our projects.
Execute these commands in the terminal
$ cd heyworld
$ python manage.py runserver
Installation of Django is Successful. I hope you find this article helpful, so if you have any questions use the comment section and feel free to leave some suggestions too.