Categories
Django

Getting Started with Django

In this post, we will get started with django framework, set up django in our local environment and briefly introduce the django folder structure.

This post will also give you insight on which type of project is suitable to built using django.

Introduction

What is Django

Django is a free and open source web framework written in python programming language.It is a fully functional frameworks meaning that it consists of all the components/libraries required to develop a web application.

At its core, Django is simply a collection of libraries written in Python programming language.

Django follows the MVT (Model View Template) architecture workflow.

django mvt arch

When a user requests for resources from url in browser, the django dispatches the urls to views which maps to respective model and serves the template as response.

Why Django

Django takes care of most of the hassle of web development, so you can focus on writing your app without needing to re-invent the wheels. Django provides readymade component to use such as authentication, authorization, forms, etc. It is mainly used because of the following features:

Truenary Solutions
  1. easy to switch database
  2. has built in admin interface
  3. is a fully functional web framework
  4. highly scalable
  5. follow DRY (Don’t Repeat Yourself) principle
  6. secure

What can you do with Django

Django was first created to power a web application for a newspaper publisher, the Lawrence Journal-World. Since that lots of features has been added. It can handle projects with great volume of media content and heavy traffic. Below is a list of project types that can be developed using django:

  1. B2B CRM (Customer Relationship Management) system
  2. Analytical Web Application
  3. E-commerce Platform
  4. Filtering Platform
  5. Social Media Platform
  6. Admin Interface and many more.

Some of the popular web application built using django are:

Instagram, Spotify, Pinterest, Bitbucket, Disqus, Dropbox, Mozilla, Udemy and many more.

What are the prerequisites to learn Django?

Before Getting into started django there are few technologies to get familiar with. Django at its core is simply a collection of libraries written in Python programming language. So you need to get comfortable with some basic Python programming language and its terminologies. Some of the prerequisites to learn before getting started with django are:

  1. Basic understanding of Client/Server Web Architecture
  2. Basic knowledge of python programming language
    • Understanding python data structure i.e. list, tuple, set, dictionary etc.
    • Python functions as it is used in views to render template
    • Get familiar with the python control statement. It includes conditions and loops.
    • Understanding of importing external modules
    • Understanding Python Object Oriented as class and inheritance can also be used in views instead of functions
  3. Understanding of web technologies such as html, css and javascript is a must
  4. Prior knowledge of database is required

Setting Up Django Environment

Now that we have basic knowledge of python and web technologies lets get started setting up django development environment.

The main requirement for django is python so the first step in installing the framework is to make sure you have python installed.

Install Django in Windows

The following are the steps to install django in windows:

Step 1: Install latest version of python

Firstly, download the latest python version from the official site https://www.python.org/downloads/windows/  according to your computer bit version (32 or 64). During installation process make sure to “Add Python to your environment variable”. Verify the installation by running the command in command prompt:

python --version 

Step 2: Setting up a virtual environment

While working with django it is best practice to use virtual environment. Virtual environment will isolate your django project. This means that the libraries in one environment will not affect in another.

For this post we will be using a new directory learnDjango inside your home directory.

Commandline:
C:\Users\UserName>mkdir learnDjango 
C:\Users\UserName>cd learnDjango  
C:\Users\UserName\learnDjango>

We will install a virtualenv package and make a virtual environment called ‘myenv’ and activate the virtual environment.

Commandline:
C:\Users\UserName\learnDjango>pip install virtualenv
C:\Users\UserName\learnDjango>virtualenv myenv
C:\Users\userNAme\learnDjango> myvenv\Scripts\activate
(myenv)C:\Users\userNAme\learnDjango>     //myenv virtual environment is activated

Step 3: Install django

Finally, Install django inside virtual environment using pip. Pip is a python package manager used to install other packages including django.

Commandline:
(myenv)C:\Users\userNAme\learnDjango>python -m pip install --upgrade pip //upgrade to latest version of pip
(myenv)C:\Users\userNAme\learnDjango>py -m pip install Django

Check if it is installed correctly

commandline:
(myenv)C:\Users\userNAme\learnDjango>django-admin --version

Install Django in MacOS

The steps are similar to windows installation but commands are different.

Step 1: Install latest version of python

In mac and linux, python 2 is already installed. So, install latest version of python. You can install python using terminal or GUI. If you want to install python using terminal you need homebrew, a package manager for mac.

In this post, we will install python by downloading the python package from official site https://www.python.org/downloads/mac-osx/ and running the downloaded package.

Note: you can verify if latest python is installed by following command in terminal

terminal:
~$python3 –version
~$pip –version or pip3 --version

Step 2: Setting up a virtual environment

You can install virtualenv with the help of pip as

~$pip3 install virtualenv 

For this post we will be creating a virtual environment called ‘myenv’ inside your home directory.

terminal:
~$python –m virtualenv myenv

you can activate the virtual environment using following command:

terminal:
~$source ~/myenv/bin/activate
(myenv) ~$

Step3: Install django

After activating virtual environemnt, you need to install django inside the environment as it will be isolated from other python package outside the environment.

terminale:
(myenv) ~$python –m pip install django

Note: the above command will install the latest django version if you want to install specific version you can specify it as: $python –m pip install django=3.0.1

create virtual env to get started with django

Creating a skeleton django project

Once you install django inside a virtual environment, all you need is to create a django project. You can create a project in any directory as long as you activate the virtual environment. For the sake of this post lets create a src directory inside myenv directory.

terminal:
(myenv) ~$ cd myenv
(myenv) myenv$ mkdir src
(myenv) myenv$ cd src
(myenv) myenv$ django-admin startproject helloworld //create helloworld project 

This will create a helloworld directory in your current directory. Now to verify if the project is working, change the directory to helloworld and run the command:

terminal:
(myenv) ~/myenv/src/helloworld$ python manage.py runserver

You’ll see the following output on the command line:

django runserver

Hence, Django server is started. The above error is because we have not setup database so can ignore it for now.

You can view response at http://127.0.0.1:8000/ from the browser

Overview of Project Structure

Now let’s look at project structure, the start-admin command creates few directories and python files as

helloworld/
manage.py
helloworld/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py

These files are:

  • The outer helloworld/ root directory is a container for your project.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
  • The inner helloworld/ directory is the actual Python package for your project.
  • helloworld/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
  • helloworld/settings.py: A Django settings file contains all the configuration of your Django installation. 
  • helloworld/urls.py: All the URL of the projects is decleared in this file. A “table of contents” of your Django-powered site.
  • helloworld/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
  • helloworld/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. Django’s primary deployment platform is WSGI, the Python standard for web servers and applications.

Django Project Vs Django App

django project refers to the entire application whereas, a django app is a submodule of the project. You might think of app as a standalone python module.

You can create a project in Django with this command:

terminal:
$ python manage.py startproject e-commerce

This will create e-commerce directory in your working directory.

And, you can create apps with these commands:

terminal:
$ python manage.py startapp cart
$ python manage.py startapp products

The structure would look like this:

e-commerce/            #project folder
    manage.py
    cart/               #app1
    products/           #app2
    e-commerce/
       

Wrapping Up

In this post, we went in detail about django and setup django in local environment. Finally, we learnt about the folder structure of django.

Hope you were able to follow it well.