Django is Python-based free and open source web framework that allows model-template-view architectural patterns. Django encourages rapid development and clean and pragmatic codes. It takes care of much of the hassle of web development so that you can focus on the code without reinventing the wheel.
- Django is exceedingly fast as the developers was focusing on quick way to release the project as fast as possible.
- Django is scalable. Building apps in Django makes it easy to scale as the traffic increases saving you on the hassle of adding additional server resources to accommodate the surge in traffic.
- Django takes care of security seriously. It has different ways of authentication which makes it hard for intruders to penetrate.
Prerequisites
- Python 3
- Virtualenv
- Pip
- psycopg2 if you are using PostgreSQL
- DB API driver like msqlclient if you are planning on MariaDB
- Database such as Postgresql, MariaDB, SQLite if planning on large scale production code.
- Basic programming skills
Related Content
Table of Contents
- Updating our system packages
- Installing Python 3
- Installing Virtualenv
- Installing Pip
Updating Ubuntu 21.10 packages
First we need to update and upgrade our Ubuntu repository so that all packages are up to date I. We can use the following command to do so.
$ sudo apt update
$ sduo apt upgrade -y
When both update and upgrade are complete then we can go ahead and start our installation of Python 3.
Installing Python 3
Django 2 doesn’t support Python 2 anymore that is why we need to install python 3 in our system. So let’s install our latest version of Python 3.10.0. Use the following comand to install Python
$ sudo apt install python3.10.0
Sample output
....
The following additional packages will be installed:
bzip2 libpython3.10-minimal libpython3.10-stdlib mailcap mime-support python3.10-minimal
Suggested packages:
bzip2-doc python3.10-venv python3.10-doc binfmt-support
The following NEW packages will be installed:
bzip2 libpython3.10-minimal libpython3.10-stdlib mailcap mime-support python3.10 python3.10-minimal
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
....
Press Y to continue with the installation. After the process is complete, you would have installed Python 3.10 minimal. You can check the version of installed Python with the following command.
$ python3.10 --version
Python 3.10.0
If you happen to check your installed version with pyhton3 –version, you will get the results of the preinstalled version, you don’t need to remove it from the system, just leave it because it is part of the system properties.
# python3 --version
Python 3.9.7
2. Install Django Virtualenv
It is important to create a virtualenv because each project you create often comes with its installed libraries, so it is important to isolate each so that dependencies would not clash at any time.
For us to create a virtualenv we need to install pip. Pip is a package installer for python. So it is necessary to have this package. Pip has two methods of installation:
- Using ensurepip
- Using get-pip.py
using ensurepip to install pip
The simpliest way to install pip with ensurepip is to pass the following command which will install pi automatically.
$ python -m ensurepip --upgrade
–upgrade. This ensures installed version of pip is the latest version.
Using python script to install pip
Python script is the most convenient way to install pip in our system. It uses bootstrapping logic to install pip. We can do the following. Download the following script from:
# wget https://bootstrap.pypa.io/get-pip.py
--2021-11-04 11:33:10-- https://bootstrap.pypa.io/get-pip.py
Resolving bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.208.175, 2a04:4e42:31::175
Connecting to bootstrap.pypa.io (bootstrap.pypa.io)|151.101.208.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2159352 (2.1M) [text/x-python]
Saving to: ‘get-pip.py’
get-pip.py 100%[===========================================>] 2.06M --.-KB/s in 0.009s
2021-11-04 11:33:10 (223 MB/s) - ‘get-pip.py’ saved [2159352/2159352]
When the script is complete then you can now install pip with the following command;
# sudo python3.10 get-pip.py
Collecting pip
Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
|████████████████████████████████| 1.7 MB 23.2 MB/s
Collecting wheel
Downloading wheel-0.37.0-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, pip
Successfully installed pip-21.3.1 wheel-0.37.0
You can also upgrade pip with the following command:
$ python -m pip install --upgrade pip
It is alaso advisable to check the pip version you are running to avoid conflicts which might arise later. Do the following;
# pip --version
pip 21.3.1 from /usr/local/lib/python3.9/dist-packages/pip (python 3.9)
It is now time we install our virtualenv in our system. We can do that with the following:
$ sudo apt install virtualenv
From now on every project you create you do it inside virtualenv. Let us first create a project folder named python_project. Inside this folder we can now install our virtualenv to work on.
# mkdir python_project
# cd pyhton_project
Inside the project folder this is where you are going to create your virtualenv.
# virtualenv venv -p python3.10
created virtual environment CPython3.10.0.final.0-64 in 251ms
creator CPython3Posix(dest=/root/python_project/venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==20.3.4, pkg_resources==0.0.0, setuptools==44.1.1, wheel==0.34.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Having created the virtualenv then we can now focus on building our project but first we need to activate our environment.
# source venv/bin/activate
You can know it is working if you can see the venv starting from the command line.
# source venv/bin/activate
(venv) [email protected]:~/python_project#
Installing Django in virtualenv
To now install django we can simple invoke install django like in the following code: Make sure you are doing everything inside project folder. If you have exited your virtualenv you need to activate and return to venv environment.
# pip install django
Collecting django
Downloading Django-3.2.9-py3-none-any.whl (7.9 MB)
|████████████████████████████████| 7.9 MB 29.8 MB/s
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
|████████████████████████████████| 42 kB 2.0 MB/s
Collecting pytz
Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
|████████████████████████████████| 503 kB 57.5 MB/s
Collecting asgiref<4,>=3.3.2
Downloading asgiref-3.4.1-py3-none-any.whl (25 kB)
Ignoring typing-extensions: markers 'python_version < "3.8"' don't match your environment
Installing collected packages: sqlparse, pytz, asgiref, django
Start Django project
Set up is now complete. To start a new project we do the following;
$ django-admin startproject python_pproject
Start Django App
Lastly to start django apps we do the following:
$ django-admin startapp <app_name>
Conclusion
In the above tutorial we have successfully installed django and learn how to create project. What is remaining now is for you to practice alot in order to grasp everything django does. In case of a problem contact Django Documentation.