Python/Django Web Development: Why I Love It

February 17th, 2011 (Guest) Leave a comment 4 comments
Like the article?
Python and Django

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.

Custom Components

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.

Custom Tags

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!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!

About Alex Dawson

Alex is a freelance web developer who has by now built a dozen of web applications using the Python and Django combo. He has written several articles on this and other subjects related to e-commerce and web hosting, which can be found on his EzineArticles author profile.

4 comments

  1. florin says:

    How some find the following exciting code is beyond me:

    {% regroup people by gender as gender_list %}
    
    {% for gender in gender_list %}
        {{ gender.grouper }}    
            {% for item in gender.list %}
                {{ item.first_name }} {{ item.last_name }}
            {% endfor %} 
    {% endfor %}
    

    Proper client side pages should be done with tags. Many such systems exist, server side (i.e. CFML, Wicket) or client side (). Here is a sample:

     1
     2   
     3     Qty
     4     Description
     5     Cost
     6     Total
     7     
     8   
     9   
    10     
    12     
    13     
    15     {{item.qty * item.cost | currency}}
    16     [<a href rel="nofollow">X</a>]
    17   
    18   
    19     <a href rel="nofollow">add item</a>
    20     
    21     Total:
    22     {{invoice.items.$sum('qty*cost') | currency}}
    
  2. James Cai says:

    Love your story! I may not have that long experience as you do in the web development but certainly I do love python/django!. Before I force myself to love java and its web frameworks but with python/django it just makes fall in love with them. Although they have some faults/annoyances it doesn’t push me away.

  3. aVisitor says:

    “too many ways too solve”… That should be “too many ways to solve”.

    “It’s object orientation”…
    That should be “Its object orientation”.

    “The hardest thing to get used to as a new Python developer is that Python uses whitespace to delimit blocks of code.” That’s just silly. First, every Python IDE automatically inserts and checks the correct whitespace, and of course balances the delimiters used for strings, sets, and lists. Second, Java requires explicit classes and “new” declarations for each variable, while Python supports “duck typing” and automatic creation; that’s a BIG difference, and hard for a Java programmer to get used to.

    The real problem here is that you try to cram two different subjects into one short article — a language and a framework — where most readers have “religious” opinions about each topic, and would appreciate deeper insights.

  4. kinderale says:

    thank you very much for the article, alex :)

Comment