I’ve been developing web sites for 15 years. In that time, I’ve used a lot of technologies and a lot of frameworks. Some of them I’ve loved and well, some of them I’ve loved to hate. While I enjoy PHP and am particularly fond of the CakePHP framework, my favorite platform for web development is the Python based Django framework.
One of the reasons I love Django so much is that it is a Python framework. Python is one of those languages that people seem to be either passionate about or indifferent. Personally, I discovered Python after working for a long time in PERL. I found that in PERL there were often too many ways too solve a problem and trying to recreate my thought process when returning to old code during maintenance was nearly impossible. Python, on the other hand, tends to have one way to do a number of things. It’s object orientation is very clean and I found a lot of Python’s constructs similar enough to Java to make it easy to learn. Python isn’t without its faults, of course. The hardest thing to get used to as a new Python developer is that Python uses whitespace to delimit blocks of code.
Django ORM and More
Django has a decent object relational mapper built in. An object relational mapper (ORM) is simply a system that manages the serialization of objects into a relational database. The syntax for Django’s ORM is very simple. It handles a number of conditions and there is the possibility to write custom SQL queries when needed. Django’s ORM isn’t without its detractors though, which is why Django is developed around the concept of loose coupling. Theoretically, a developer could replace Django’s default ORM with a different system, SQL Alchemy is one example, without needing to modify the rest of Django. This is one area where theory and practice differ. Many of Django’s out of the box systems rely on the Django ORM. In my experience, however, Django’s default ORM has been more than adequate for the types of tasks I needed to perform.
Speaking of some of Django’s out of the box solutions, these are one of the strong points behind my love of Django. The user authentication system I’ve found particularly useful. It is full featured enough that for most projects, it is trivial to create a user log in system. The permissions for users are very easy to extend as well. In fact, when compared to more complex Auth and ACL systems such as the one in CakePHP, I’ve found the Django system to be extremely simple and straight forward.
Another big benefit of Django is the ability to create custom middleware components. In Django, the middleware components act on all requests to provide some additional processing. This can take a number of forms and Django comes with a number of built in middleware classes. The Auth Middleware, handles retrieving the logged in user from the session and exposes the user object as part of the session. Speaking of the session, Django also comes with a session middleware that populates a session object as part of the request. Middleware can also be used for a number of other tasks. For example, in a blog, you may have permalinks based on a page slug stored in your database. You may have a middleware component that will take a URL and look for a matching post in your database. If one is not found, the middleware will raise a 404 error.
One area that I’ve always had trouble with in CakePHP is when I have a template for my pages that requires some content from one of my models. In CakePHP, you need to add the data from the model to your context somehow. This usually involves writing a “mini-controller” or using RequestAction. Django has, in my opinion, a more elegant solution. You can create custom template tags in Django. For example, in one application, I built a custom tag that would select a random image from a database of images and display it. I could then load this custom tag library in any of my templates and add random images. This ability to extend my templates with custom tags is something I’ve found that I use a lot in my Django development.
There are a number of other features of Django that I really love. The template language is very simple but powerful. URL routing is very configurable and flexible. I could almost wax poetic about my love of Django but the features I’ve discussed are the ones that stand out the most to me and they are the features that have saved my butt in more than one development project. Have you tried Django or used it? What are your favorite features?
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Jan 25-27||Programming in Python|
|Mar 29-31||Programming in Python|
|Programming in Python|
|- Classroom - Online|