(For more resources related to this topic, see here.)
I would like to introduce you to Django by using a definition straight from its official website:
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
The first part of this definition makes clear that Django is a software framework written in Python and designed to support the development of web applications by offering a series of solutions to common problems and an abstraction of common design patterns for web development.
The second part already gives you a it clear idea of the basic concepts on which Django is built, by highlighting its capabilities on rapid development without compromising the quality and the maintainability of the code.
To get the job done in a fast and clean way, the Django stack is made up of a series of layers that have nearly no dependencies between them. This introduces great benefits as it will drive you to code with almost no knowledge sharing between components making future changes easy to apply and avoiding side effects on other components.
All this identifies Django as a loosely coupled framework and its structure is a consequence of the just described approach and can be defined as the Model-Template-View (MTV) framework, a variation of the well know architectural pattern called Model-View-Controller (MVC). The MTV structure can be explained in the following way:
Model: The application data
View: Which data is presented
Template: How the data is presented
As you can understand from the architectural structure of the framework one of the most basic and important Django components is the Object Relational Mapper (ORM) that lets you define your data models entirely in Python and offers a complete dynamic API to access your database.
The template engine also plays an important role in making the framework so great and easy to use—it is built to be designer-friendly. This means the templates are just HTML and that the template language doesn’t add any variable assignments or advanced logic, offering only “programming-esque” functionality such as looping.
Another innovative concept in the Django template engine is the introduction of template inheritance. The possibility to extend a base template discourages redundancy and helps you to keep the information in one place.
The key to the success of a web framework is to also make it possible to easily plug third part modules in it. Django uses this concept and it comes—like Python—with “batteries included”. It is built with a system to plug in applications in an easy way and the framework itself already includes a series of useful applications that you can feel free to use or not.
One of the included applications that makes Django successful is the automatic admin interface, a complete, user-friendly, and production-ready web admin interface for your projects. It’s easy to customize and extend and is a great added value that helps you to speed up most common web projects.
In modern web application development, systems are often built for a global audience, and web frameworks have to take into account the need to provide support for internalization and localization.
Django has full support for the translation of text, formatting of dates, times, and numbers, and time zones, and all this makes it possible to create multilingual web projects in a clear and easy way.
On top of all these great features, Django is shipped with a complete cache framework that is a must-have support in a web framework if we want to grant great performance with high load. This component makes caching an easy task offering supports for different types of cache backends, from memory cache to the most famous, memacached.
There are several other reasons that make Django a great framework and most of them can be really understood by diving into the framework, so do not hesitate and let’s jump into Django.
Installing Django on your system is very easy. As it is just Python, you will only need a small effort to get it up and running on your machine. We will do it in two easy steps:
Step 1 – What do I need?
The only thing you need on your system to get Django running is obviously Python. At the time of writing this book, the latest version available is the 1.5c1 (release candidate) and it works on all Python versions from 2.6.5 to 2.7, and it also features experimental support for Version 3.2 and Version 3.3.
Get the right Python package for your system at http://www.python.org. If you are running Linux or Mac OSX, Python is probably already installed in your operating system.
If you are using Windows you will need to add the path of the Python installation folder (C:Python27) to the environment variables.
You can verify that Python is installed by typing python in your shell. The expected result should look similar to the following output:
Python 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Step 2 – Get and install Django
Now we will see two methods to install Django: through a Python package manager tool called pip and the manual way. Feel free to use the one that you prefer.
At the time of writing this book the Django Version 1.5 is in the release candidate status, if this is still the case jump to the manual installation step and download the 1.5 release candidate package in place of the last stable one.
Installing Django with pip
Install pip; the easiest way is to get the installer from http://www.pip-installer.org:
If you are using a Unix OS execute the following command:
$ sudo pip install Django
If you are using Windows you will need to start a shell with administrator privileges and run the following command:
$ pip install Django
Installing Django manually
Download the last stable release from the official Django website https://www.djangoproject.com/download/.
Uncompress the downloaded file using the tool that you prefer.
Change to the directory just created (cd Django-X.Y).
If you are using a Unix OS execute the following command:
$ sudo python setup.py install
If you are using Windows you will need to start a shell with administrator privileges and run the command:
$ python setup.py install Django
Verifying the Django installation
To verify that Django is installed on your system you just need to open a shell and launch a Python console by typing python.
In the Python console try to import Django:
>>> import django >>> django.get_version() '1.5' c1
And that’s it!!
Now that Django is installed on your system we can start to explore all its potential.
In this article we learned about what Django actually is, what you can do with it, and why it’s so great. We also learned how to download and install Django with minimum fuss and then set it up so that you can use it as soon as possible.
Resources for Article :
- Creating an Administration Interface in Django [Article]
- Creating an Administration Interface with Django 1.0 [Article]
- Views, URLs, and Generic Views in Django 1.0 [Article]