Django A Powerful Python Framework – Introduction

The purpose of this article is to guide you about programming language named python. Why python is powerful and useful, the history of python, basic arithmetic operations and how datatypes are represented in python. Then in later part we will see what frameworks are available in python.

Getting Started with python

If you are using Mac and running Mac OS X, Then your luck is on seventh sky because mac is having Python preinstalled, But version must be up-to-date. For security and compatibility purposes, I would suggest logging on to www.python.org/download/mac. Check to see that your Mac OS X version is the right version for the Python you are installing. If you have linux machine then you may have python installed but always remember to get latest version of python from www.python.org/download All downloading and installation instructions are there.

A simple program

Simply open Notepad or any Text Editor and start writing your first python code.

print(“This is a sample string”)

print(“we can join two strings using “ + “the plus operator”)

Now you added this first program to your text editor, try and save it. First, go to File, then Save As

A pop-up menu appears, saying you for a name and directory in which to save your file. Python files use the extension .py, so always be sure to add it to the end of your file name, otherwise Notepad will save it as its default type, .txt. Give it the name first.py. Next, navigate to the directory where Python is installed. Normally, this will be something along the lines of C:/Python31/. Click the Save button and you are all set.

Please notice some amazing things. First, when you initially opened the first.py file, Python took the liberty of highlighting your code in different colors. This makes functions and data types (and many other programming tidbits) easier to recognise. For instance, the print() function is in purple color, whereas the string that comprises its value is green quite simple to understand.

when you run or execute this code you will not see the code but the output, written out in blue text on your screen:

This is a sample string

we can join two strings using the plus operator

Do this a few more times with different strings, saving them in different files.

To combined two strings we have to use the format specifier %s.

Basic Math In Python

Its more common than not that youll have to use the numbers in your program in basic arithmetic. Addition, subtraction, division, and multiplication are all built in. Addition and subtraction are performed by the + and symbols.

You can enter basic arithmetic at the Python shell prompt and use it like a calculator. Like a calculator, Python will accept a set of operations, and when you press the Enter key, it will evaluate everything youve typed and give you your answer:

Commonly being a programming language python supports all basic arithmetic operations, Addition, subtraction, division, and multiplication are all built in. Addition and subtraction are performed by the + and symbols. When you enter basic arithmetic at the Python shell prompt you can use it like a calculator.

>>> 5 + 200

205

>>> 399 + 3020 + 3456

6875

>>> 300 - 59994 + 19

-59673

>>> 4023 - 22.46

4000.54

In addition to + and , multiplication is performed by the asterisk, *, and division is performed by the forward slash, /. Multiplication and division may not be as straightforward as youd expect in Python, because of the distinction between floating-point numbers and whole numbers.

Make a note that although Python can deal with some very large numbers, the results of some operations will exceed what Python can accommodate. The shorthand for infinity, inf, is what Python will return when a result is larger than what it can handle.

There is one other basic operation of Python that you should be aware of: the remainder, or modulus operation. A new addition to Python is the ability to view the entire result of a piece of division. Previously, if you wanted to know the remainder you had to use the modulus operator, because Python would show only the whole number portion of the answer. For 324/101, Python would have displayed 3. In some instances, believe it or not, you still need only the remainder portion of a division result. To find this part of the answer, you have to use the modulus operator, which is the %. Dont let this confuse you! The % means modulus only when it is used on numbers. When you are using strings, it retains its meaning as the format specifier.

>>> 5 / 3

1.6666666666666667

>>> 5 % 3

2

generally 5 divided by 3 is 1.6666666666666667, and in this example we can see that when you divide 5/3, you have a remainder of 2. One very useful task the modulus operator is used for is to discover whether one thing can be evenly divided by another, such as determining whether the items in one sequence will fit into another evenly.

You need to be careful when you are dealing with common floating-point values, such as money. Some things in Python are puzzling. For one thing, if you manipulate certain numbers with seemingly straightforward math, you may still receive answers that have extra values trailing them, such as the following:

>>> 4023 - 22.4

4000.5999999999999

The trailing nines could worry you, but they merely reflect the very high precision that Python offers. However, when you print or perform math, this special feature actually results in precise answers.

Python is having one other feature with its numbers that is worth knowing about so that you understand it when you encounter it. The normal counting system that we use is called base 10, or radix 10. It includes numbers from 0 to 9. Numbers above that just involve combining 0 through 9. However, computers commonly represent the binary numbers they actually deal with in base 8, called octal, and base 16, also called hexadecimal.

In advanced python development practices you must be aware that python is loaded with best object oriented capabilities you can define objects classes and achieve your programming goals,

Python in web applications

Here we will see what is advantage of Django (A python Based Framework) and how it works.

Unlike PHP, Java, .NET No doubt Python can also provide you a web development friendly framework and yes now a days Django is favourite in developers and contractors are looking forward to deploy their enterprise level applications using Django. Lets have little introduction of it.

The first foundation of web application is database connectivity This is a vital part of any web application. It contains many records you may not even think about. Here is where you will store information like user name, permissions, settings, comments, profiles the list goes on and on. Django supports quite a few databases, some of which do and do not use SQL.

The second foundation is the administrative panel. An administrative panel gives the admin, and others, the ability to work with anything stored in the database. For instance, if you want to change a users permissions from a registered user to a super administrator, you will need an admin panel.

The Third foundation is ability to leave comments. Because comments entertains you and users too and it makes pleasant to work and encouraging to your users to discuss in the way of comments on your web application.

Another important feature of this type of site is user authentication. This controls how the users sign in, ensures secure logins, decides who has permission to do what on the site, and so forth.All of the above, and more, can instantly be set up thanks to Django.

Some of the important features of Django

  1. URL Mapping.

  2. Caching.

  3. Templating

  1. URL Mapping : In apache this module is popularly known as mod_rewrite, In PHP based framework wordpress this is known as permalinks here in Django python is used and it makes it easier for search engines to understand the underlying structure of your website.
    Lets see few examples.

Suppose your web URL is
http://www.itshacked.com/cat.cgi?catname=books&bookname=I-love-python

URL Mapping rewrites it to
http://www.itshacked.com/category/books/I-love-python/

You can note that this can hide actual path of files and GET paramaters used in program which is also a major security advantage.

2. Caching : Web caching is the process of storing a copy of a document. When this page is revisited, if certain criteria are met, the page is loaded from memory, instead of a new page being requested. This, in turn, increases the speed by which the page loads and the overall usability of the site.

3. Templating : In old ages suppose your site have 1000 workers you have to create 1000 pages to display their information on websites. In the modern world languages like Python gives you a single page template where you can manage a global stylesheet and of course HTML5 programming tricks and all users data is fetched from database and 1000s of pages are created dynamically.

Django, pronounced with a silent dand rhyming with Bang-o, was named after the gypsy jazz guitarist Django Reinhardt. It was created by a group of programmers at a little place called World Online, which at the time was the department responsible for web design for the Lawrence Journal-World newspaper in Kansas, among other properties.

Installing Django

To install Django, go to http://www.djangoproject.com/download/

For Windows users, installation is fairly simple. Download the .tar.gz file and extract it to your Python26 folder (this is typically located at C:/Python26).

To install Django on non-Windows computers, such as Linux, Mac OS X, or other UNIX-based systems, download the tar.gz file and untar it with:

tar xzvf Django-1.0.0-final.tar.gz

Django Architecture

Django is MVC based. The distinction of presentation, logic, and data is obvious in most modern application logic. The distinction allows code to be separate from presentation (theme-ing HTML via CSS) and data (how data is accessed from the database and organized, and so on). The most common way this is achieved in application code is MVC (Model-View-Controller) architecture, which most web frameworks adhere, too. Java, Ruby on Rails, and Seagull are the most widely used and Django is not behind. Django use MVC.

Python is Stackless

The standard Python code interpreter is written in C. Every Python action is somehow performed by some C code. Whenever a piece of Python code is executed, a new incarnation of the interpreter loop is created and executed by a recursive call.

First of all, let’s see what it means to have a C stack. Consider a Python function a(), which calls a Python function b(), which calls a Python function c(). In the context of function c(), the C interpreters of all three functions are still alive. They are keeping their state on the C stack. When the Python functions come to an end or an exception is raised, the C functions are popped off the C stack. This is called “unwinding the stack”.

In that sense, Python is not so different from other C programs, which are usually all stack based. But this is not the full story, since Python does quite a lot more than using the C stack.

Every running piece of Python code also has an associated Frame object. A Frame object is something like a running instance of a code object. Frames are used to hold local and global variables, to maintain the value stack of the byte code interpreter, and some other housekeeping information. (Figure 5)

These Frames are chained together in a last-in/first-out manner. They make up a stack by themselves. And they do this in a way quite similar to the C stack.

Python With AJAX

The idea of rapid web development using python and AJAX was driven by two main factors: a fast turn around with changes, including user interface, use cases, DB schema, etc.; and support of user interaction with the DBS. Python had a great set of tools to accommodate our tasks. Among a large number of possibilities we choose the following components:

SQLAlchemy, the python SQL toolkit and Object Relational Mapper, as SQL abstraction layer, which provides us transparent access to different DB back-ends (ORACLE, MySQL, SQLite);

CherryPy, the python web application framework;

Open RICO framework, a JavaScript libraries which served all AJAX communications
between web server and DB back-end;

Really Simple History (RSH) JavaScript library framework, for session based history of JavaScripts user calls;

Cheetah template framework, written in python for presentation layer of web pages.
It is worthwhile to mention that those components were already used in other open source python web development frameworks. Those web application frameworks were designed to accommodate all user needs, including data management. In our case the data model was designed and developed independently and data discovery was designed to be a read-only service. When it was feasible to adopt one or another web application framework we leaned towards re-using individual software components.
This stack of software components fits very well in (MVC) architecture where we separated our data model from presentation layer. As we mentioned already, we used an SQL abstraction layer. Even though, depending on implementation, it can be slow with respect to plain DB access, our access pattern was not too high. If we take the size and distributed nature of CMS collaboration we would expect not more then a few hundred access calls during a working day per user, with 100 users per time zone. The SQLAlchemy also took care of pooling database connections. Even though we did not yet come up with a query cache it can be done very easily. We also auto-load the DB schema from back-end DB which not only simplifies our life with frequent DB schema changes, but allows us to create a dynamic advanced query interface.

It has proven that he simplicity, flexibility and power of python allowed us to concentrate on the usability of the service, by re-writing different stack components in our software model. The modern AJAX technology gave our users an application-like behaviour of web interface and the ability to integrate multiple CMS web services, such as Production system, Site DB, Complex Database oriented applications. We can say python is emerged as light weighted and highly secured web application engineering language in modern era.