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 manage.py 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 manage.py runserver 8080 <\span>

>$ python manage.py runserver 0.0.0.0:8080 <\span>

Migrate Project

>$ python manage.py migrate<\span>

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

>$ python manage.py 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 manage.py startapp <\span>

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

>$ python manage.py startapp timesheet

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

timesheet/
__init__.py
    admin.py
    migrations/
    __init__.py
    models.py
    tests.py
    views.pys

Steps to Add Models

Create Model

Open models.py, 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
name

Activate Model

Open project/settings.py, and add the models under Installed Apps:
mysite/settings.py
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 manage.py makemigrations timesheet

>$ python manage.py migrate

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

Create Views

Open views.py 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 urls.py

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

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


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

Register Models in admin.py

Open admin.py 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") admin.site.register(Department, 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.

Verification

You can run below commands to check whether the model is added successfully to your project.
>$ python manage.py 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;

Conclusion

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.