Setting up Django

Requirements for the course

⌨ Basic knowledge of python is required. (Basic syntax declaration)

⌨ Functions in python

⌨ A snapshot of Model View Controller Architecture is good knowledge for this course

⌨ A general knowledge of programming is actually required.


Setting up django

In order to use django, you will need python, pip, and a virtual environment on your machine. We will go step by step into the details of installing these software on our systems. I will be considering two different systems primarily: Windows and Linux systems, if you use Mac, you could try out the instructions for Linux.


Windows Installation

Check for Python

The simplest way to test for a Python installation on your Windows server is to open a command prompt (click on the Windows icon and type cmd, then click on the command prompt icon). 


Once a command prompt window opens, type python and press Enter. If Python is installed correctly, you should see output similar to what is shown below:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Note The part with 3.7.0 above depends on the python version installed on your system and therefore may differ. If you receive a message like:

Python is not recognized as an internal or external command, operable program or batch file.

This means either of two things:

  1. Python is either not installed  or
  2. The system variable path hasn’t been set.

For 1, You’ll need to either launch Python from the folder in which it is installed or adjust your system variables to allow Python to be launched from any location. For 2, We need to install python by ourselves.


Download Python

To install python on your windows, visit https://www.python.org and download python ( preferably, version 3.6 or more ), while installing, please make sure  you mark the global option and mark add Python to system PATH

These  will make sure it is installed across your computer, so you could run it from anywhere.

Now if you type python in your command line (please, close the command line and open again after the installation), it should show the appropriate python version you downloaded alongside some other messages as I said earlier

Now that we have python and probably PIP, let's make sure we have PIP.

Install pip

Usually Python3 comes with pip pre-installed. If you get an error "pip command not found", after typing "pip" in your command line then use the following 

steps to install pip:


Download get-pip.py (search for it), make sure you're saving the downloaded file to Desktop. In your Command Prompt navigate to Desktop like this:

cd Desktop

Execute get-pip.py like this:

python get-pip.py

Now pip should work system wide when you type pip in the command line

With that, we have 2 out of 3 of our system's requirements to install django. The third is virtualenv, let's get that too.


Installing Virtualenv

In your Command Prompt enter:

pip install virtualenv

That will get you the virtualenv package and after that we can now create a virtual environment so we start working.

Creating a virtual environment

open your command line if it's not already opened, and navigate to Desktop if you are not already there. We use cd (change directory) together with the destination directory name to navigate to that directory.

Like this:

cd Desktop

We then create a virtual environment by calling the installed virtualenv package together with a name we choose to give our virtual environment. Like this:

virtualenv myenv

The virtualenv package will create a new folder (which is our virtual environment folder) in the Desktop directory. After this creation, in order to enter our new environment, we need to activate it like this:

myenv\Scripts\activate

You should notice that "(myenv)" is now written just before the rest of the directory name in your command line.


Congratulations, Windows user, you have finished installing python, pip and virtualenv

Installing Django

Now that we have the system requirements, let's get django into our virtualenv which we created (named myenv) and activated. To get django, please type the following:

pip install django

This will get django into your virtual environment and this means if your virtual  environment is not activated, all codes using django will fail. That's no problem, just remember to activate your virtual environment before you start working anytime. The trick to know that it is activated is to check for "(name)" before the directories names in your terminal/command line.

We shall also install a package called whitenoise, this package will enable us to be able to serve static files for our website or web apps. Static files are those files that are not written codes; they usually include CSS, JS, Image files. 

To install whitenoise:

pip install whitenoise

Congratulations again, now we have our system setup to dive into django in the next tutorial.


Installation On Linux

Download Python

Linux systems usually come with python already installed on them. So first, check for the existence of python. Open up your terminal (CTRL + ALT + T, should do the trick) and type python3. Getting something like this:

Python 3.6.5 (default, May 11 2018, 13:30:17) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Means you already have python version 3 on your machine.If not, use this commands to get python.

sudo apt-get install python3

Installing pip

sudo apt install python3-pip

The above command should get pip installed for you. To check for pip version, enter:

pip3 --version

Installing Virtualenv

In your terminal enter:

pip3 install virtualenv

That will get you the virtualenv package and after that we can now create a virtual environment so we start working.

Creating a virtual environment

open your command line if it's not already opened, and navigate to Desktop if you are not already there. We use cd (change directory) together with the destination directory name to navigate to that directory.

Like this:

cd desktop

We then create a virtual environment by calling the installed virtualenv package together with a name we choose to give our virtual environment. Like this:

virtualenv myenv

The virtualenv package will create a new folder (which is our virtual environment folder) in the Desktop directory. After this creation, in order to enter our new environment, we need to activate it like this:

source myenv\bin\activate

You should notice that "(myenv)" is now written just before the rest of the directory name in your command line.

Congratulations, Linux user, you have finished installing python, pip and virtualenv

What are Python, PIP and Virtualenv ?

Python

Python is a programming language used to tell a computer how to do certain tasks. An in depth treatment of python can be found on our python course.

PIP

PIP is a package managing tool for python, it is used to install python packages or codes written by other people so that we can use them in our codes. Django, Flask, among other big projects can be downloaded unto our local machine using PIP.

VIRTUALENV

This is used to create a whole new environment within our computer, it makes sure that whatever is happening within that environment does not affect the whole system. Think of it as a sound proof room, where you can enter and shout but no one within the main house will hear you. Packages that are installed here cannot be used without activating the environment. So you need to remember to always activate, not create, activate, your virtual environment anytime you decide to work on your project.

If it is active, you should see it's name like this (myenv) before the rest of the directory name.

 Installing Django

Now that we have the system requirements, let's get django into our virtualenv which we created (named myenv) and activated. To get django, please type the following:

pip3 install django

This will get django into your virtual environment and this means if your virtual  environment is not activated, all codes using django will fail. That's no problem, just remember to activate your virtual environment before you start working anytime. The trick to know that it is activated is to check for "(name)" before the directories names in your terminal/command line.

We shall also install a package called whitenoise, this package will enable us to be able to serve static files for our website or web apps. Static files are those files that are not written codes; they usually include CSS, JS, Image files. 

To install whitenoise:

pip3 install whitenoise

Congratulations again, now we have our system setup to dive into django in the next tutorial.