Django Revolution in Python

We all know , Python is the high-level dynamic programming language widely used in software industry. In addition to its readability and simplified concepts and syntaxes, great advantage of Python is it is available as open source. Django the high-level python framework has made python development easier than before. Django is again a free and open source, which helps developers to build a web application ridiculously fast, with high security and scalability.

This article is to help a beginner to understand the basic nature of Django and how to create a simple application using the inbuilt features in Django.

Create Python Virtual Environment

“virtualenv” is a tool to create isolated Python environments. “virtualenv” creates a folder, which contains all the necessary executables to use the packages that a Python project would need. If you have python already installed in your system, then execute below command to install virtual environment:

> $ pip install virtualenv

The below command will create a folder, which act as a isolated place to execute or create python project with version 3.x

$ virtualenv --python=python3 mypython3

Navigate to mypython3 folder, you can see folders like bin, include, lib. Run below command to create working space with python.

>$ . bin/activate

Run below command to verify python version.

>$ python --version <\span>

you can see the system displays python 3.x version. It confirms we are in required python3.x environment.

Install Django

We need to choose proper Django version with respect to the python version we installed in our system or virtual environments. It is always good to go with stable versions. The below commands installs and check the version of django.

>$ pip install django==1.10.5
>$ django-admin --version

Create Django Project

Create a django project within your virtual environment folder.

>$ django-admin startproject myproject <\span>

=> where myproject is projectname

Check Project

Navigate to your project

>$ python runserver<\span>

default port will 8000, you can view project page in http://:8000/
you can also run below command to choose your IP’s and port

>$ python runserver 8080 <\span>

>$ python runserver <\span>

Migrate Project

>$ python migrate<\span>

On successful migration you can now navigate to login page http://:8000/admin
run below command to create super user:

>$ python createsuperuser<\span>

provide superuser username and password, and try login to your project.

Create App

We can add app as separate folder within the project.
$ python startapp <\span>

e.g., if your app name is ‘timesheet’, then

>$ python startapp timesheet

Now you can see a sub folder created within the project as like below,


Steps to Add Models

Create Model

Open, and add class and fields you preferred to add in your application:

from django.db import models

class department(models.Model):

deptname = models.CharField('Department Name', maxlength=100, null=False)

deptcode = models.CharField('Department Code', maxlength=20, null=False)

is_active=models.BooleanField('Is Active', default=True)

def _str(self):
return self.dept

Activate Model

Open project/, and add the models under Installed Apps:
'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'timesheet', )

Adding Models to Database

Since we have added a new model, we need to include the model in our database, to get it done run below commands:
>$ python makemigrations timesheet

>$ python migrate

we need to execute the above migration commands, whenever there is change in the model and table.

Create Views

Open and add your scripts as shown below:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Department

def department(request):
department = Department.objects.all() return render(request, template_name="department/index.html", context={"department":department})

Add Views in

Create a new file in your app folder (timesheet), add below codes in the

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^department$', views.department, name='department'), ]

Register Models in

Open file in your app folder (timesheet), and add below scripts:

from django.contrib import admin
from .models import Department
class DepartmentAdmin(admin.ModelAdmin):
listdisplay = ("deptname", "deptcode", "isactive"), DepartmentAdmin)

Now execute migration and runserver commands and browse http://:/admin/, on logging in you can see a new model “Department” is added to your admin page. You can click Department link to add new departments.


You can run below commands to check whether the model is added successfully to your project.
>$ python dbshell

sqllite> .tables
the .tables command will list all the tables in the project, which should include timesheet_department table as well. You can also add new departments from your app and run below command to verify the data in database.
sqllite> Select * from timesheet_department;


At the time you reach this section, I hope you found the easiest way to build your own application. Again, this article covers only the basic parts in Django, there are more awesome features available, and I recommend everyone to go through them to build your applications.