Irrational Exuberance!

Reflections on Django (repost)

June 1, 2007. Filed under djangowriting

This is a transplant from the original Irrational Exuberance, and was written in mid 2007: nearly two years ago. It was my first article to be read outside of my immediate group of friends and was picked up on Reddit programming. It is unquestionably quaint, but I only started proofreading my blog entries in the past six months, so I won't profess a particularly strong attachment to perfection. A friend recently asked me to throw together my thoughts on using Django as a development framework, so here is an attempt to oblige him, and also solidify my opinion on Django in general. For the past two (maybe three...) weeks Django has been at the forefront of my day, each and every day, for most of the day. When I began, I had never used Django before, but at this point I have read a fair quantity of its source (which is something of a requirement, when you want to do something tricky, as their documentation--especially on newer components--is spotty), and would consider myself fairly competent at using the framework. So, without further ado, here we go.

Easy to learn

One of the best things I have to say about Django, is that it is fairly easy to pick up. Throw some time its way, and it will reciprocate kindly. My previous project before this one (which may also be my next project after this one, silly interruptions) used the PyObjC framework to write Cocoa applications for OS X in Python. Getitng used to their framework was really frustrating for the first 3-4 days. It had a high learning curve (especially lacking anyone I could easily turn to and ask them to point out where I was being a complete doofus), and Django simply doesn't have a painful learning curve like that. If you work through their tutorial, you'll derive a fantastic solid understanding of how to use their framework, and reading their online documentation can get you 97.2% of the way to a finished product. Its only when you do relatively tricky stuff (or use a new module) that you'll have to dive into source code.So, really easy to learn, thumbs up to Django.

Sufficiently powerful

Anything I wanted to do in developing my webapp, Django was more or less prepared to facilitate me. Sometimes doing it exactly how I wanted involved a lot of code though. My views.py (Django uses a modified MVC paradigm, but the views.py file more or less corresponds to thew MVC concept of views) at one point was almost 1000 lines long. I have since seperated my forms into a seperate file (forms are essentially a way of visually representing a model, so my inclusion into the views.py file was only moderately awful), and am planning on breaking the views.py apart into a package (actually, I broke it apart last night, the content creation system is definately the bulk of the views, coming in at about 500-600 lines, often the Django Admin would allow you to skip writing this code, but not in my case), but still, to have high granularity control over your data, you end up writing a lot of views.Its worth pointing out that they do have a generic template system, but it didn't really have the flexibility and power that I needed for my project. I imagine that many simple projects that emphasis displaying data rather than creating it would be trivially simple to write in Django (more thoughts on this in the Django Admin section later). Worth point out that the views are not difficult code to write, but sometimes I'd rather write difficult code than slog through a bunch of filling (PyFilling? is that a project name already? probably).So again, this is favorable towards Django, although its a bit aggravating at times, it can do what you need it to, or at least it learns new tricks easily.

Django Admin

The Django admin is really staggeringly helpful to work with--when you first get started. Its always amazing to get so much functionality "for free." It is a fantastic tool. That said, it is not very flexible, and if you want to do heavy modifications to it, the recommendation is essentially to roll your own. Blah. There is a branch project working on a more flexible Django admin tool (i.e. granular permissions), but I expect that it'll be a long time coming. Even if they do finish it, it isn't clear that they are using newforms (Django is in the process of switching from their old implemention of forms to a new one, but currently many of their modules still use oldforms) (there is a newforms admin branch project too, weee), so it may be a long time coming.My verdict on the Admin is that its fantastic if you want to use it for what it can do, and is a total disaster to try to extend in a meaningful way (I tried for a while, admittedly while I was still learning the Django ropes, but it was a massacre, and everyone lost).

Middleware, especially Authentication and Sessions

The Authentication/Sessions middleware are fantastic. They let me implement a login/logout/registration system really quickly and easily. There are generic views for these activities, although I chose not to use them (they use oldforms, and I was sticking entirely with newforms, and the benefit from mixing seemed too low to justify the confusion). Really easy to use, and fantastically helpful.I should also mention that the Caching middleware is also fantastic. I setup caching for my website in approximately ten minutes, after reading the documentation for maybe 20-30 minutes to figure out exactly what I wanted to do (although, I am slightly apprehensive that it may be caching more than I want it to, I may have to use the granular caching system that caches only specific pages you specify).Simply put, the middleware are incredibly helpful tools that I can't recommend enough.

Great community

Django has a fantastic and lively community. #django is always lively, and is usually pretty helpful (although, getting redirected to the same bad example on djangosnippets.com can be moderately vexing at times ;P). There are a bunch of fairly helpful guides, mailing lists, and other resources available for Django, so this is a plus for Django too.

Debugging Django is okay

Django debug/error messages are fairly helpful. Occasionally I get angry at the error screen, but since they provide the entire python traceback, you can usually figure out the problem. Certainly easier to debug than PyObjC stuff (not to harp on them, I love the project, its just painful sometimes).

Differences between Devel server and Apache

I had a devious error when I transfered my app from the Django development server to my production environment (an Apache server). For whatever reason the newforms stopped correctly validating data. Well, they *sort of* validated the data correctly, but they stopped populating the clean_data field within the form. I have no idea why this happened, and I am guessing its because the newforms are relatively new and still under development, but it was fairly frustrating to have to hack a worse solution to a problem I had already solved (on the devel server).

Beta Software

Many of the inconveniences surrounding Django stem from the fact that it is indeed Beta software. This includes my complaints about newforms, the admin being inflexible, etc. I suppose it shouldn't surprise me that much, but sometimes I'd just like it to be a bit more polished.

Summary

In the end, I really like using Django. Its flexible, powerful, and fun to use. It has definite rough spots around the edges, but I'd recommend charging towards them--thats how you'll get to really understand the nature of the beast (or at least improve your code-reading). I am considering using it again in another project, but I may be inclined to use it more for data presentation projects instead of complex web apps in the future. I hear TurboGears is an excellent framework for web applications, so I may try that out eventually. What I'd *really* like to try out is the Lift platform, which is written in Scala, and appears to scale far better than any other comparable product (except maybe something like CakePHP, but I don't know if I can ever consider a php product to be comparable to a framework in python/ruby/scala).Anyway, hope my ramblings were moderately informative, and I'd love to hear any feedback.