Thursday, March 5, 2015

Get start with Django ?


This Blog is about How to do Web Application in Django frame work with Linux 
Operating System , a Web development framework that saves you time and makes Web development a joy.Using Django, you can build and maintain high-quality Web applications with minimal fuss. With Django, you can take Web applications from concept to launch in a matter of hours. Django takes care of much of the hassle of Web development,so you can focus on writing your app without needing to reinvent the wheel. It’sfree and open source. 

Getting started with Django

     What you need for a basic dev environment:

    • Python 2.7.x or Python 3.4.x
    • easy_install and Pip
    • virtualenv
    • Django
    • Database (SQLite, MySQL, PostgreSQL, MongoDB, etc.)
    • Text editor (Sublime, vim, Komodo, gedit)
   Details for basic dev environment

  • Python

       Python is an easy to learn, powerful programming language. It has efficient  high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its   interpreted nature, make it an ideal language for scripting and rapid application              development in many areas on most platforms.More info :

  • Easy_install and Pip
     Easy Install and Pip is a python module (easy_install and pip ) bundled with setuptools that lets you automatically download, build, install, and manage Python packages.

  • Virtualenv
     A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.virtualenv is a tool to create isolated Python environments.

  • Django
       Django is a free and open source web application framework, written in Python. It's a web framework - a set of components that helps you to develop websites faster and easier.Django's primary goal is to ease the creation of complex, database-driven websites. Django emphasizes reusability and "pluggability" of components, rapid development, and the principle of don't repeat yourself. Python is used throughout, even for settings, files, and data models. Django also provides an optional administrative create, read, update and delete interface that is generated dynamically through introspection and configured via admin models.

Django installation

Before we install Django, we'll get you to install an extremely useful tool that will help keep your coding environment tidy on your computer. It's possible to skip this step, but it's highly recommended not to - starting with the best possible setup will save you a lot of trouble in the future!
So, let's create a virtual environment (also called a virtualenv). It will isolate your Python/Django setup on a per-project basis, meaning that any changes you make to one website won't affect any others you're also developing.

Create a virtual Env Iinux Operating System

On Linux, pip will generally be available for the system install of python using the
system package manager, although often the latest version will be unavailable.
Install Pip (Python Package Index )

       Step1 : sudon apt-get install python-pip

       Install Virtual Env In your Local machine

       Step 2 : pip install virtualenv

      Create  Virtual Env 

      Step 3 :  

         step 1 :  mkdir global ( Create a global directory for Environment ) 
         step 2 : cd global ( Set the Dirctory Root ) 
         step 3 : virtualenv MYENV ( Environment Name )
         step 4 :   source global/MYENV/bin/activate(  Activate the Environment  )

     Installing Django

         Step 4 : pip install Django ( This command will install latest version for Django )

    Django Project setup

        Step 1 : startproject myfirstproject ( project name )

  These files are

  •  The outer myfirstproject/ root directory is just a container for your project.
  • A command-line utility that lets you interact with this  Django            project in various ways. You can read all the details about in       and
  • The inner myfirstproject/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. myproject.urls).
  • myfirstproject/ An empty file that tells Python that this directory should be considered a Python package. 
  • myfirstproject/ Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • myfirstproject / The URL declarations for this Django project; a “table of contents” of your Django- powered site. You can read more about URLs in URL dispatcher.
  • myfirstproject / An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
Let’s verify your Django project works. Change into the outer mysite directory, if you haven’t already, and run the following command:

python runserver

You’ll see the following output on the command line:
system checks...
System check identified no issues (0 silenced).
March 05, 2015 - 07:45:37
Django version 1.7.4, using settings 'myfirstproject
.settings' Starting development server at
Quit the server with CONTROL-C.

that the server’s running, visit with your Web browser. You’ll see a “Welcome to Django” page, in pleasant, light-blue pastel. It worked!

Create Your first App

Each application you write in Django consists of a Python package that follows a certain convention. Django
comes with a utility that automatically generates the basic directory structure of an app, so you can focus on
writing code rather than creating directories.

python startapp myfirstapp


Get Start With Django Project:

       I am going to start a simple project for personel blog webapplication , already i create a virtualenv MYENV , nowI am going to install little more packages in MYENV using Pip command.Pip install PIL , Pip install Pillow and Pip install django-imagekit ( this package for mangeing the Images )pip install ipython ( Ipython is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language )

Create a project :
 startproject myblog

create a application

         python startapp blog


import os
def root(x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# Application definition
# Database
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': root('db.sqlite3'),
STATIC_URL = '/static/'
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
TEMPLATE_DIRS = [ root('templates')


from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
urlpatterns = patterns('',
url(r'^admin/', include(,
url(r'^', include('blog.urls', namespace="blog")),
) urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) urlpatterns += staticfiles_urlpatterns()

from imagekit.processors import ResizeToFill
from imagekit.models import ImageSpecField
# Create your models here.
class Blogs(models.Model):
b_name = models.CharField(max_length=200,null=True,blank=True)
heading = models.CharField(max_length = 500,verbose_name='Heading',null=True,blank=True)
date = models.DateField(verbose_name = "Published Date",null=True,blank=True)
category = models.CharField(max_length=200,verbose_name='Blog Category',null=True,blank=True)
publisher= models.CharField(max_length=200,verbose_name='Blog Publisher',null=True,blank=True)
mage = ProcessedImageField(upload_to='images/blog', options={'quality': 60},verbose_name = "Blog Image (828px X 363px)",null=True,blank=True)
b_content = models.TextField(verbose_name='Blog Content',null=True,blank=True)
active = models.BooleanField(default=True,verbose_name="Blog Active or Not")

def __str__(self): # __unicode__ on Python 2
return self.b_name

from django.contrib import admin
from .models import Blogs,

Database Migration 

           python makemigrations blog
            python migrate

Create a Admin User
       python createsuperuser
       Enter your name : Admin
       password : qwerty

Run your Project

       python runserver

Now that the server’s running, visit Enter username and password , then you can see the fields for adding blogs in myblog website. Add some data in to Blogs table

Create Your Views
from django.shortcuts import render
from blog.models import Blogs
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
all_blog = Blogs.objects.all().order_by('-id')
return render(request,'index.html',data)


from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
Create Template folder inside the myproject Directory ( if any static files is there in your template create a folder static inside the myproject for uplodaing staticfiles(css,js and etc ) , make sure if any static files is there you have to run the command Python collectstatic , this command will create a folder static storage , it have replica of static folder if any any chages you made in static files make sure run the command Python collectstatic,

Create a Template


<div class="content">
<div class="blog-section">
<div class="container">
<div class="col-md-8 left_column">
<h2 class="with-line">Blogs</h2> {%for blog in all_blog%}
<article class="clearfix">
<header class="grid_8 alpha">
<div class="head_element"><span class="el_1">{{|date:"d"}}</span><span class="el_2">{{|date:"b"}}</span>
<div class="sub">
<a href="single.html" class="readblog">{{blog.heading}}</a>
<p class="sub_head">Posted by <a href="#">{{blog.publisher}}</a>
<div class="clearfix"></div>
<div class="alpha1">
<a href="single.html"><img src="{{blog.image.url}}" alt="">
<div class="grid_b4">
<h5> category : {{blog.category}}</h5>
<p style="text-align:justify;">{{blog.b_content|truncatewords:50}}</p>
<a href="{% url 'blog:single'}" class="btn btn-1 btn-1c">Read More</a>
<div class="clearfix"></div>
<div class="clearfix"></div>


      Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.


      Now that your app is set up, follow either of these simple workflows every time you want to make changes to your app:

  • Navigate to your project
  • Activate virtualenv
  • Create and checkout a new git branch
  • Develop
  • Commit changes
  • Merge the new branch with your master branch
  • PULL the changes into the production folder
  • Deploy.



  1. I like your blog, I read this blog please update more content on python, further check it once at python online course