How to install Python Django in Linux OS

How to install Python Django in Linux OS

How to install Python Django in Linux OS. Django is a Python-based free and open-source web framework that follows the model template view (MTV) software architectural pattern. Django’s version of the model–view–controller (MVC) design is known as the MTV pattern. The model is the sole definitive source of your data, the view describes the data that is shown to the user via a Python callback function to a specific URL, and the template is how Django dynamically generates HTML, according to the Django Software Foundation.

Scalability, reusability, and quick development are three of Django’s basic ideas. It’s also notable for its framework-level consistency and loose coupling, which allows individual components to function independently. Django’s principles include “don’t repeat yourself” (DRY programming).

Introduction

We’ll build up a Django development environment in this tutorial. To give you with the tools you’ll need to develop web apps with Django, we’ll install Python 3, pip 3, Django, and virtualenv.

What is Linux os?

The Linux® operating system is a free and open source alternative to Windows® (OS). The software that controls the hardware and resources of a computer, such as the CPU, memory, and storage, is known as an operating system. Between apps and hardware, the operating system connects all of your software to the actual resources that accomplish the work.

How does Linux work?

Consider a computer’s operating system to be the engine of an automobile. An engine can run on its own, but it becomes a functional car when it is connected to a transmission, axles, and wheels. The rest of the car won’t work until the engine is up and running.

Linux began as a UNIX-like operating system, but it has since evolved to run on a broad range of devices, including phones and supercomputers. Every Linux-based OS includes the Linux kernel, which manages hardware resources, as well as a set of software packages that make up the rest of the operating system.

The OS includes several common core components, such as the GNU utilities. These programmes enable users to manage kernel resources, install additional software, tweak performance and security settings, and more. When all of these tools are merged, the operating system is formed. Different Linux distributions may include different software combinations because Linux is an open source operating system.

Prerequisites

On a Debian or Ubuntu Linux server, a non-root user account with sudo capabilities. These prerequisites can be met by following and finishing the first server setup instruction for Debian 8, or steps 1-4 in the initial server setup tutorial for Ubuntu 16.04.

Step 1 — Install Python and pip

To install Python, we must first update the APT repository on our local machine. We’ll type the following command into your terminal window. The -y flag responds to prompts with “yes” during the upgrade process. If you want the upgrading to halt at each prompt, remove the flag.

sudo apt-get update &&sudo apt-get -y upgrade

When prompted to configure grub-pc, click ENTER to accept the default configuration or customise as needed.

The Django Software Foundation recommends using Python 3, so if everything is up to date, we may install Python 3 with the command:

apt-get install python3 sudo

Run a version check with the python3 command to ensure that Python 3 was successfully installed:

python3 -V 

This is what the final output will look like:

Output

“python 3.5.2”

We’ll need pip to install packages from PyPi, Python’s package repository, now that we’ve installed Python 3.

sudo apt-get install -y python3-pip

Run the following command to see if pip was installed successfully: 

pip3 –V

You should get something like this:

Output

“pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)”

Now that we have pip installed, we can rapidly install the rest of the Python environment’s required packages.

Step 2 — Install virtualenv

virtualenv is a virtual environment that allows you to install software and Python packages in a separate development space that is isolated from the rest of your machine’s global environment. This handy separation keeps conflicting packages or applications from interfering with one another.

We’ll use the pip3 command to install virtualenv, as seen below:

“pip3 install virtualenv”

After it’s been installed, conduct a version check to ensure that everything went smoothly:

virtualenv –version

We should see anything along these lines, or something similar:

Output

virtualenv 20.0.20 from /home/sammy/.local/lib/python3.5/site-packages/virtualenv/__init__.py

You have completed the installation of virtualenv.

We can now separate our Django web application and its software dependencies from other Python packages or projects on our machine.

Step 3 — Install Django

Django can be installed in three different methods. For this guide, we’ll use the pip method of installation, but we’ll go through all of the other choices just in case.

  • Option 1: Install Django within a virtualenv.

This is useful when you need to keep your Django version separate from the rest of your server’s environment.

  • Option 2: Download and install Django from the source code.

You can install directly from source if you want the most recent software or anything that isn’t available in the Ubuntu APT repository. If you want to keep your software version up to date, this installation method necessitates ongoing attention and maintenance.

  • Option 3: Use pip to install Django globally.

We’ll use pip 3 because we’ll be installing Django on a global scale.

We’ll use pip to install Django in a virtual environment. Check out this lesson on setting up a virtual environment for more help and information on how to set up and use programming environments.

We must construct the directory that will hold our Django application while in the server’s home directory. Create a directory called django-apps (or whatever name you like) with the following command. After that, go to the directory.

mkdirdjango-apps
cd django-apps

Create your virtual environment inside the django-apps directory. Let’s name it env 

virtualenvenv

Now, use the following command to start the virtual environment:

. env/bin/activate

It’s active when the prefix is changed to (env), which will look something like this depending on which directory you’re in:

Install the Django package using pip within the environment. We can construct and run Django applications after installing Django. Read our Django Development tutorial series to learn more about Django.

install django with pip

Run a version check on your Django installation after it’s been installed: 

django-admin –version

The following, or something similar, will be the end result:

Output

2.2.12

We may create a test project now that Django is installed on your server to ensure everything is working properly.

Step 4 — Creating a Django Test Project

We’ll make a skeleton web application to test the Django installation.

Creating Firewall Policies

First, we’ll need to open the port we’ll be utilising in our server’s firewall, if necessary. If you’re using UFW (as described in the first server setup instructions), use the following command to open the port:

sudoufw allow 8000

You can choose HTTP from the inbound rules if you’re using DigitalOcean Firewalls. Read the inbound rules part of the beginning tutorial to learn more about DigitalOcean Firewalls and how to create rules for them.

Starting the Project

Using django-admin, a command-line programme for Python administration duties, we can now construct an application. The startproject command can then be used to construct the project directory structure for our test website.

Run the following command while in the django-apps directory:

django-admin startprojecttestsite

Note:

“When you perform the django-admin startproject<projectname> command, it will name both the project directory and the project package <projectname> and create the project in the directory where the command was run. Django will utilise the specified destination directory as the project directory and create manage.py and the project package within it if the optional <destination> parameter is provided.”

We can now look at the project files that were recently produced. To examine what files were created, go to the testsite directory and list the contents of that directory:

testsite cd
ls

Output

manage.py testsite

This directory has a file entitled manage.py and a folder named testsite, according to the output. The manage.py file, like django-admin, adds the project’s package to the sys.path. This also sets the DJANGO SETTINGS MODULE environment variable to the settings.py file in your project.

Using the less command, you can see the manage.py script on your terminal:

less manage.py

When you’re done reading the script, press q to exit the programme.

To see the other files that were created, go to the testsite directory:

cd testsite/

Then, to list the contents of the directory, use the following command:

ls

There are four files to look at:

Output

__init__.py settings.py urls.py wsgi.py

Let’s go over each of these files one by one:

  • The entry point for your Python project is __init__.py
  • settings.py explains your Django installation’s configuration and tells Django which options are available.
  • The urlpatterns list in urls.py routes and maps URLs to their views.
  • The setup for the Web Server Gateway Interface is found in wsgi.py. The Python platform standard for the deployment of web servers and applications is the Web Server Gateway Interface (WSGI).

Note:

“Despite the fact that a default file was generated, you can change the wsgi.py at any time to suit your deployment needs.”

Start and View your Website

Using the runserver command, we can now start the server and see the website on a certain host and port.

In the settings.py file , we’ll need to add your server’s IP address to the list of ALLOWED HOSTS  the settings.py is located in

~/test_django_app/testsite/testsite/.

The ALLOWED HOSTS variable includes “a list of strings representing the host/domain names that this Django site can serve,” according to the Django documentation. This is a security technique to protect against HTTP Host header attacks, which can occur even in seemingly secure web server installations.”

You can add your IP address with your favourite text editor. If you’re using nano, for example, simply run the following command:

“~/test_django_app/testsite/testsite/.”

After you’ve ran the programme, go to the Allowed Hosts section of the page and type your server’s IP address in square brackets within single or double quotations.

settings.py

“””

Django settings for testsite project.

Generated by ‘django-admin startproject’ using Django 2.0.

“””

# SECURITY WARNING: don’t run with debug turned on in production!

DEBUG = True

# Edit the line below with your server IP address

ALLOWED_HOSTS = [‘your-server-ip’]

By holding down the CTRL + x keys and then pressing the y key, you can save your changes and quit nano.

After you’ve finished this, return to the directory where manage.py is located:

cd ~/django-apps/testsite/

Replace your-server-ip with your server’s IP address and run the following command:

python manage.py runserver your-server-ip:8000

Finally, you can view what your skeleton website looks like by going to the following link and replacing the highlighted text with your server’s actual IP:

“http://your-server-ip:8000/”

You’ll see a webpage after the page loads.

This ensures that Django was installed correctly and that our test project is up & running.

After you’ve finished testing your programme, use CTRL + C to terminate the runserver command. You will now be back in your programming environment.

You can use the deactivate command to exit your Python environment when you’re ready:

“deactivate”

You can return to the terminal command prompt by deactivating your programming environment.

Conclusion

You have successfully upgraded to the most recent version of Python 3 accessible via the Ubuntu APT repository in this guide. You’ve also got pip 3, virtualenv, and django installed.

You now have all of the necessary tools to begin developing Django web applications.

Leave a Comment

Your email address will not be published.