Skip to main content

Django - Some Basics

Django, a prominent member of new generation web framework lets you build deep, dynamic, interesting sites in an extremely short time. Django is designed to let you focus on the fun, interesting parts of your job while easing the pain of the repetitive bits. Django provides high-level abstractions of common Web development patterns, shortcuts for frequent programming tasks, and clear conventions on how to solve problems.

Django, as a Web framework provides a programming infrastructure for your applications, so that you can focus on writing clean, maintainable code without having to reinvent the wheel. 

The MVC Design Pattern

Django follows a pattern called Model-View-Controller(MVC). Simply put, MVC is a way of developing software so that the code for defining and accessing data (the model) is separate from request-routing logic (the controller), which inturn is separate from the user interface (the view). While using Django framework, the entire code is split into four python files as models.py, views.py, urls.py and an HTML template file.
  • The models.py file contains a description of the database table, represented by a python class. This class is called a model. Using it, you can create, retrieve, update and delete records in your database using simple python code rather than writing repetitive SQL statements.
  • The views.py file contains the business logic for the page.
  • The urls.py file specifies which view is called for a given URL pattern.
A key advantage of such an approach is that components are loosely coupled. Each distinct piece of a Django-powered Web application has a single key purpose and can be changed independently without affecting the other pieces. For example, a developer can change the URL for a given part of the application without affecting the underlying implementation. A designer can change a page's HTML without having to touch the python code that renders it. A database administrator can rename a database table and specify the change in a single place, rather than having to search and replace through a dozen of files.

Installing Django

Django itself is written purely in Python, so the first step in installing the framework is to make sure you have Python installed. Official releases have a version number, such as 1.0.3 or 1.1, and the latest one is always available at http://www.djangoproject.com/download/
In the case of linux you have to download the tarball, unzip it and then run setup.py install.  The steps to be followed are given below.
  1. tar xzvf Django-1.0.2-final.tar.gz
  2. cd Django-*
  3. sudo python setup.py install

Testing the Django installation

To check whether Django is installed successfully, start the python interactive interpreter by typing python. If the installation was successful, you should be able to import the module django.

>>>import django
>>>django.VERSION
(1, 1, 0, 'final', 1)

Setting up a Database

Django supports four database engines:
Setting up the Database is a two-step process:
  • First, you’ll need to install and configure the database server itself. If you’re on a shared hosting provider, odds are that they’ve set this up for you already.
  • Second, you’ll need to install the Python library for your particular database backend. This is a third-party bit of code that allows Python to interface with the database.

Starting a Project

Once you have installed python, Django and your Database server/library, you can step in developing a Django application by creating a project. A  project is a ccollection of settings for an instance of Django, including database configuration. Django-specific options and application-specific settings. The startproject command creates a directory containing four files:

mysite/
   __init__.py
   manage.py
   settings.py
   urls.py
These files are as follows:

  • __init__.py: A file required for python to treat the project directory as a package (i.e., a group of python modules). Its an empty file, and generally you wont add anything to it.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. Type python manage.py help to get a feel for what it can do. You should never have to edit this file. Its created in this directory purely for convenience.
  • settings.py: Settings for the Django project. Take a look at it to get an idea of the types of settings available, along with their default values.
  • urls.py: The URL for the Django project. Think of this as the table of contents of your Django-powered site.

Running the Development Server 

The Django development server is a built-in, lightweight Web server you can use while developing your site. IT's included with Django so that you can develop your site rapidly, without having to deal with configuring your production server until you are ready for production. The development server watches your code and automatically reloads it, making it easy for you to change your code without needing to restart anything. To start the server, change into your project directory, if you haven't already, and run this command.
python manage.py runserver
You will see something like this:
Validating models....
0 errors found.
Django version 1.0, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C
This launches the server locally, on port 8000, accessible only to connections from your own computer. Now that it's running, visit http://127.0.0.1:8000/ with your Web browser. You will see a "Welcome to Django" page shaded in a pleasent pastel blue which indicates thet it is working.

Comments

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Elements of Programming in Scheme

A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this. primitive expressions , which represent the simplest entities the language is concerned with, means of combination , by which compound elements are built from simpler ones, and means of abstraction , by which compound elements can be named and manipulated as units. Some very basic ideas in manipulating with the data are given below. Expressions   A very simple expression is a number. Simple expressions are combined to form complex ones. For example:                   (+ 2  3  4) is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:              ...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...