What is Django and How to Install It?

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. Django adheres to the model template view (MTV) software architectural pattern. The MTV pattern is Django’s take on the model–view–controller (MVC) Pattern.

Step 1 — Install Python and pip

Before install any software, first update APT repository. Then install software as per your requirements.

# sudo apt update

Once everything is update now you can install python3 in your machine.

# sudo apt install python3

To verify that pip was successfully installed, run the following command:

# python3  --version 

You should see output similar to this:

Now we need to install Pip in order to install package from Pypi (Python package repository).

# sudo apt install  python3-pip -y

To verify that pip was successfully installed, run the following command:

# pip3 --version

You should see output similar to this:

We have successfully install python and pip. Now we could install the package for Python Virtual Environment.

Step 2 — Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained development space, which isolates the installed software and packages from the rest of your machine’s global environment. This convenient isolation prevents conflicting packages or software from interacting with each other.

Now we can install package using pip3 command.

# pip3 install virtualenv

Once it is installed, run a version check to verify that the installation has completed successfully:

If in case you have already install then you would get this type of output.

Now, we can isolate our Django web application and its associated software dependencies from other Python packages or projects on our system.

Step 3 — Install Django

1.    Install latest version Django within a virtualenv

2.    Install different version of Django  within a virtualenv.

While in the server’s home directory, we have to create the directory that will contain our Django application. Run the following command to create a directory called django-projects, or another name of your choice. Then navigate to the directory.

# mkdir django-projects

# cd django-projects

While inside the django-projects directory, create your virtual environment. Let’s call it env1.

# virtualenv env1 --python=python3

 

Now activate the environment using these commands.

 

# source ./env1/bin/activate

 


You’ll know it’s activated once the prefix is changed to (env1), which will look similar to the following depending on what directory you are in:

Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications.

# pip3 install django

 

 

Once installed, verify your Django installation by running a version check:

# django-admin --version


Step 4 — Creating a Django Test Project

Starting the Project

We now can generate an application using django-admin, a command line utility for administration tasks in Python. Then we can use the startproject command to create the project directory structure for our test website.

# django-admin startproject demo1

Now we can look to see what project files were just created. Navigate to the demo1 directory then list the contents of that directory to see what files were created:

Now, run the following command to check your django project.

# python3 manage.py runserver

Now copy this url and paste in brower you would get first page of django.

Step 5 - Install different version of Django  within a virtualenv.

Create a new virtual environment and activate the environment using following commands.

# virtualenv env2 --python=python3

Now activate the environment using these commands.

 

# source ./env2/bin/activate

 


You’ll know it’s activated once the prefix is changed to (env2), which will look similar to the following depending on what directory you are in:

 

Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications.

 

Now install a different version of django.

 

# pip3 install Django==2.2.16

Once installed, verify your Django installation by running a version check:

# django-admin --version

Step 6 — Creating a Django Test Project

Starting the Project

We now can generate an application using django-admin, a command line utility for administration tasks in Python. Then we can use the startproject command to create the project directory structure for our test website.

# django-admin startproject demo2

Now we can look to see what project files were just created. Navigate to the demo2 directory then list the contents of that directory to see what files were created:

Now, run the following command to check your django project.

# python3 manage.py runserver


Now copy this url and paste in brower you would get first page of django.

Get more information about python training and certification course visit here.

Best Python Training Institute in Gurgaon...

Best Python Training Institute in Noida...


Thank You For Reading

Written by Sachin Saini

Comments