Home Audience Developers Django: A Web Framework with Immense Potential

Django: A Web Framework with Immense Potential

2
6139

In the era of cloud computing, Web frameworks have great importance in a Web developer’s life. Choosing the right one is crucial to the success of a Web application. This article explores one such (not so) popular Web framework Django.

The success of a Web application depends on factors like managing high traffic, maintaining site code, quick updates, bug tracking, ready-made tools, code re-usability, help for code development, etc all of which become important in a Web framework. There are a lot of Web frameworks available that have been written in various programming languages. In reality, only a few are used extensively and are recommended by the industry. So what is so special about those few?

Django is a framework developed for Lawrence.com by Simon Willison and Adrian Holovaty. It is written in Python. Every Web framework is an effort to counter identified loopholes in an existing technology. Django was developed for the same reason. Initially, at Lawrence.com, code was being written in PHP, but code maintenance was a big headache for developers. That’s when Willison and Holovaty, both fond of Python, decided to write a Web framework based on Python. One of the basic features of Django is that it’s a URL-driven Web framework. Developers could design their own URLs, and choose what was to be shown for that URL, quite unlike the frameworks existing at the time.

As I mentioned earlier, when it comes to choosing the right Web framework, there is now a problem of plenty. Since there are many options, comparison is inevitable. I do not intend to make this article a battle between Django and other frameworks. I would rather aim at discussing a few points that forced me to use Django as our core Web framework for AppSurfer (http://www.appsurfer.com).

Coding standards: Code readability is one of the most important aspects of software development. It becomes crucial when more than one person works on a code piece. Python believes in The One True Wayi.e., there should be a single way of writing any code. Therefore, rather than two or three different options on syntax, method calls, etc, there’s usually one option in Python.

Python: Django is based on Python, and hence benefits from all the advantages of Python. Python has a clean and elegant syntax, and is supported by a large library of standard and contributed modules, which cover everything from multi-threading to the zipping of files. The language’s object-oriented model is especially suited for MVC-style development. Sooner or later, performance will become a major concern with Web projects, and Python’s runtime environment shines here, as it is known to be fast and stable. Python supports a wide range of Web servers through modules, including the infamous Apache. Furthermore, it is available for all the major platforms: UNIX, Linux, Windows, and Mac. Python also supports a wide array of database servers, but you won’t have to deal directly with them; Django provides a unified layer of access to all available database engines.

MTV architecture: MTV architecture: M stands for Model, the data  access layer, which contains  everything about the data how to access it, how to validate it, its characteristics, and the relationships between data. T stands for Template, the presentation layer, which contains presentation-related decisions how something should be displayed on a Web page. These are HTML files with some Django tags which are processed while being rendered. V stands for View, which basically acts as controller which renders an HTML page using templates and models.

A Robust ORM system: Django has a powerful Object-Relational Mapping system. It supports a large set of database systems, and switching from one engine to another is a matter of changing a configuration file. This gives the developer great flexibility if a decision is made to change from one database engine to another.

Beautiful URLs: Compare a URL from any Django website to those of an ASP or PHP site, and you’ll notice a big difference. You can read the Django URL. And that’s good for you, and Google, since its factored into the pages importance. Some Web frameworks come close to Django in URL construction. But what makes Djangos URLs extra-nice is the ease with which you can create them.
 
A powerful template system: Django has a very powerful template system to inject programming logic inside templates. The framework is great at simplifying complex things, and the template system is no exception. It is clear and easy to adopt, which speeds up development.

Automatic admin interface: Django comes up with a ready-made admin interface, which allows a developer to control Web applications via a Web portal. This admin interface is generic, and can be extended and customised, depending upon project requirements.

Complete development environment: Django provides a complete development environment. It comes with a lightweight Web server for development and testing. When the debugging mode is enabled, Django provides very thorough and detailed error messages, with a lot of debugging information which, in turn, makes debugging easier, and allows one to quickly isolate bugs.

A rapidly growing community:  The Django community is growing really fast. A strong community is always helpful when you need some help, training, reusable modules, utility tools, etc. Django has its own packages for almost all types of reusable modules like error logging, automated deployments, social networking integrations, blogging and so on.

Django project architecture (Django 1.3)
An empty Django project folder structure (see Figure 1) contains three important files:
settings.py: This contains project settings, such as database connection information, email server configuration, installed apps, media folder location and static folder location with their URLs, template folder location and  reference to some Django modules.

urls.py: Can be considered the DNS of our Django app. Each request to the application goes through urls.py, and entries in the urlpatterns list in this decide which request is to be forwarded to which view of a particular app.

manage.py: Contains commands that, as the name suggests, are used for managing our Django project for example, startapp, syncdb, etc.

Each Django Web application comprises small modules called apps. If we consider Wikipedia as our project, the project would contain user as one app, which would handle all functionality related to user management. Page would be another app, handling all page-related functionality, and would be used by the user app, since each page is related to a user that created or modified it.
Each app inside our project contains three files:
models.py: models.py: This is the place for all your application data (that you want to be persistent). These are basically classes extending models.Model, which is provided by Django ORM library. It is recommended that business logic should be written in models so that it remains intact with models and can be used by any other app of your Django project.

views.py: This is the place where you render HTML pages for the HTTP requests that are redirected to particular view function via urls.py.Basically, views contains functions, each of which get the HTTP request as first parameter. They get data from models and render HTML pages using Templates, or redirect to another view function.

tests.py: This is the place for your test cases. These are functions performing unit test cases. This file should be effectively used for regression testing (to check the side effects of your code changes).

The flow of an HTTP request
To explain the flow of an HTTP request through a Django Web application, let us look at my personal website, http://akshaydeo.me. As Figure 2 shows, the HTTP request is sent to the Web application’s urls.py, where URL-regular expressions are sequentially applied to it; the first match is accepted, and the request is forwarded to the corresponding view. In views.py, that particular function performs the required processing, which might include communication with models in the database; then, the HTML template is rendered, and the output returned to the browser as the result of the request.

Before concluding this article, I would like to mention one site whose valuation has shocked everyone Instagram, which was developed in Django. Instagram was one heavily used site, and each transaction had to be processed in real time. But the power of Django, and Instagram’s engineering talent, made everything possible. So keep coding, and start using Django!

References
[1]  http://www.quora.com
[2]  http://www.blog.pythonlibrary.org/2011/12/07/python-voted-best-programming-language-3-years-running/
[3]  http://djangoproject.com

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here