CSC/ECE 517 Fall 2014/ch1a 8 sn

From Expertiza_Wiki
Revision as of 20:07, 14 September 2014 by Sbastik (talk | contribs)
Jump to navigation Jump to search

Django Framework

Django is a high level, but heavy, Python based web framework used to build a large scale web application. It deals with the most important problem in the domain of Web development – repetition. Even though web development is a really interesting task, there are a lot of things that utilize unnecessary effort and time of the developers as well as the testers. Django provides high-level abstractions of common Web development patterns, shortcuts for frequent programming tasks, and clear conventions to solve problems. With an MVT Framework, Django is very modular and thus very much suitable for highly cohesive and low coupling tasks.


History

In 1998, Python CGI was used to develop web applications from scratch using Python. In order to create a web application using Python CGI, we just have to create a Python script that generates output in the HTML format. We then save the script to a Web server with a “.cgi” extension and visit the page in your Web browser. Following is the sample way of creating a simple website using the script.

#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html\n"
print "<html><head><title>Food items</title></head>"
print "<body>"
print "<h1>Food items</h1>"
print "<ul>"
connection = MySQLdb.connect(user='username', passwd='password', db='food_db')
cursor = connection.cursor()
cursor.execute("SELECT food_item_name FROM food_table ORDER BY rating DESC LIMIT 10")
for row in cursor.fetchall():
	print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()

The above script generates an HTML page which will display a list of all the food items in the descending order of their rating by interacting with the respective table in the database.

Even though the code has several advantages which include:

  • Writing from the scratch doesn't seem like a bad idea for a script like this.
  • This code is easy to understand even for the novice developers to read and modify.
  • Easy to deploy as we just have to save the file with "*.cgi" extension, upload it to a web server of your choice and host it. You will be able to access the web page from your browser.

But even though it seems simple, there are numerous disadvantages to this approach. To name a few:

  • When we have a multi-page web application, which is very common these days, all of these pages might want to access the database. Replicating the database connecting code in every page is not a good practice.
  • Accessing the Distributed databases with different credentials will create a mess in using the CGI script approach because that needs some environment specific configuration.
  • There are several important tasks associated with the web applications such as closing the database connection, including necessary tags in each of the files etc. which is a very repetitive and time consuming task.

Django had a solution to all of these and many other problems that the developers and the testers face during the different phases of web development. It is a Web framework that provides a programming infrastructure for your applications, so that the developers can focus on writing clean, maintainable code without worrying about the different aspects of Project Scaffolding. Django is DRY (Don't Repeat Yourself). DRY aims at reducing repetitive pieces of information of all kinds in software development.

Other Frameworks

  • Flask: An open source micro web framework that provides only the core libraries of web development. It does not provide database abstraction, form validation or any other component that can be provided a third-party component.
  • Pyramid: An open source minimalistic web framework inspired by Zope, Pylons and Django. It strictly adheres to the MVC (Model-View-Controller) pattern and persistence agnostic.
  • web2py: An open source macro web framework inspired by Ruby-on-Rails. It follows Ruby-on-Rails' convention over configuration approach and provides sensible defaults for various components and libraries.


Features

Django has the following features to sum it all:

  • It contains the basic modules, classes, and tools to quickly develop and deploy web apps
  • It contains an ORM (Object-Relational Mapper) that allows for the use of standard Python language syntax when interfacing with a back-end database.
  • Developer need not learn SQL, DDL, etc!
  • It provides a template framework that allows HTML, XML, and other “documents” to be converted into “templates”, which can then be converted to “output” via a wide range of substitution techniques.
  • It is Fast and easy to use, robust, flexible, and lots of contributed components available.
  • It has built in administrative interface to manage data models.
  • It has built-in authentication/access control components
  • It has contributed geospatial support components (GeoDjango)
  • It is Extensible.

Django - MVT pattern