fb ad

Thursday, 22 September 2016

Getting started with Django for beginners the right way - part 1

Introduction

If you are a beginner in programming or you are starting with Django, I am going to explain everything right from basics i.e. from installing whatever packages that are required to finally getting your website live on the web.. :)

What will you learn

  • Setting up your machine for Django development
  • Creating applications in Django
  • Running your website locally
  • Deploying website online 

Let's get started


What is Django???
Django is a web framework written in Python which helps people like us in building websites easily. In other words, it has some pre written codes for signing in/out users, database management, etc to make our life simple. :)

Installation

Its generally preferred to use a linux system such as Ubuntu for development. So if you are not using one then you should consider installing linux.
(Django can work windows also. This wont be covered in this tutorial)

Installing Python
For Django to work, you will need some other packages such as Python. If you are on a Linux system such as Ubuntu then you might already have Python as it comes inbuilt with the OS. Check if python is installed by opening terminal (ctrl+alt+T) and type
python --version
It should display the version of python currently installed. If not, then install it by typing
sudo apt-get install python

Installing pip

Also you will need pip package which is used to install other packages easily. Python usually comes with pip installed. Check if pip is installed by typing
pip
in the terminal. If you see list of all pip commands, then you have pip installed on your system. Otherwise go on the link https://bootstrap.pypa.io/get-pip.py and download the get-pip.py file. Once the file is downloaded, open the terminal and change to the directory in which the file is downloaded. Use the command 
cd path-to-directory
where path-to-directory is the path of the directory in which the get-pip.py is located. Mostly it will be downloaded to Downloads folder if you are on Ubuntu and you can use
cd Downloads
to change to download folder. Once you are in the folder, type
python get-pip.py

to install pip on your system.

Installing virtualenv

Its better to do everything in virtual environment i.e. a folder which you can deploy directly. Otherwise you code might be actually be dependent on the things installed on you system and might not work when deployed online.
To install virtualenv type
sudo pip install virtualenv
in terminal. Once the virtualenv package is installed, change the directory to the folder (as explained above) where you want all your project files to be present. Make a folder for all your Django projects by typing
mkdir djangowork
in the terminal where djangowork is the folder name which will be created and will have all the projects files that we will work on later. You can name it as per your choice. Then change directory to that folder and type
virtualenv djangovenv
in the terminal to make a virtual environment. Here djangovenv is the virtual environment name and can be anything you like. Once this is done, change directory to the virtual environment by typing
cd djangovenv
Then activate virtual environment with the following command
source bin/activate
Once the virtual envt. is activated,you should see the envt. name in brackets at the starting of each line in terminal.

Stuck? Comment below and I'll try to help.

Installing Django

Finally.. Now that we are inside the virtual environment, all the things that we will install will only be available inside the virtual envt. To install Django type
pip install django
Done. All set up for coding.. Oh wait.. Django doesn't come with its own code editor. Well you can write code in text editors like gedit, but there are more efficient code editors out there. Lets quickly install a code editor.

Installing SublimeText Editor

Simply go on this website https://www.sublimetext.com/3 and choose the right package for you. It will download a .deb file which will open in Ubuntu software centre if you are using ubuntu or just Google how to install .deb package. Once installed, you can open it by typing
subl
in the terminal. 
(If you are installing sublimetext from terminal then make sure you deactivate the virtual envt. by typing
deactivate
so that the editor is installed as a system package and can be used even when the virtual envt. is not active.
Again you can activate the virtual envt. by changing to the virtualenv directory and using the following command as we used earlier.)
source bin/activate
Finally we are done with setting up the environment. Follow up in the next part of this tutorial where we will work on Django and start building our website.


Thank You