what can you do with django?

December 2nd, 2007

So I'm reading about Django, which is a really groovy web framework for Python. As the Django people themselves say, there have been *a lot* of web frameworks for Python alone, and it's clear that a lot of lessons have been learned along the way. Personally I've only looked briefly at Zope before, but I was a bit intimidated by how complicated it seemed to be. I also once inherited a project that used Webware for Python, which was pretty nice to work with, but radically different from what I'd known because pages are constructed using a sort of abstract html notation (basically every html tag is a function etc).

There is a lot to like about Django. The MVC pattern is executed quite elegantly (or as they call it in Djangoland, the MTV (model - template - view) pattern). Templates are written in what resembles a server side language, but it's actually a specialized, both restricted and enhanced, Python-like templating language. Templates can inherit from each other to fill in placeholders in a cascading manner, very interesting design choice.

Then there is a nice separation between objects and persistence. Data is declared in terms of Python classes (called "the model") which are then translated somehow on-the-fly to sql-du-jour depending on your database backend. You can even run a Python shell to interact with your model to de facto edit your database.

The model and the template mechanism are both tied together with "views", which is the application code you write for your webapp. A cool thing here is that urls are decoupled from execution code, and the two things are mapped via regular expressions, so you have something like a mod_rewrite declaration (but nicer) that dispatches to functions.

All the same, if I were to work with Django I have a couple of concerns up front:

  1. Templates are written in html with inline Python-line expressions. This makes them both simple and transparent, because the html code that will be served is written by hand. However, considering the present situation with html and browser support, it would be nice if Django could enforce/validate html. But this would probably imply an abstraction over html (to prevent raw html being written) and a layer of code generation for html (which may or may not be easy/possible to cache efficiently).
  2. And that brings me to the second point. I have seen no mention of CSS anywhere. It appears that Django leaves the fairly heavy burden of supplying styles to the designer, without any intelligent mechanisms to help.
  3. Data models are represented in Python code, which is very convenient and provides a much sought after quick access to the contents of your database. Django will instantiate tables for you based on a model, but only for creation. It doesn't seem to enforce or update the correspondence between the Python model and the database schema. The Django book has a mention of strategies for keeping things in check, but this particular section is missing.

There is no doubt Django embodies a lot of what we've learnt about building web applications, but currently I would say it seems a bit incomplete (also pre v1.0 still), if terribly elegant in some parts.

Another question concerns performance. Ruby on Rails has been known to suffer when pitted against the effective but ugly php. Does Django fall in the same bracket or does it have a lower overhead? In general Python is an older and more refined environment, so one might expect Django to scale better.

:: random entries in this category ::

3 Responses to "what can you do with django?"

  1. tante says:

    @1: Templates are "rendered" with a given "context" (the stuff you want to insert into them). If you wanted to make sure that the rendered code it valid, you can just pipe the rendered data through something like "tidy". There are shortcuts in django like "render_to_response("template",context)" but those just simplify default tasks. No problem at all to write something similar that does use tidy to create valid code (or does at least check and sends te administrator an email of some function returns invalid code)

    @2: CSS is a designer's job, what does it have to do with the application writer? The guys writing the model and the view shouldn't need to know about CSS, just the guy writing the actual template. And I don't see where django could automate things there. You can of course use any CSS/JS framework you want with django without any problem, but the MVC thingy is exactly what decouples the templates (where the CSS matters) from the rest.

    @3: There's some work done on a few apps that offer "migrations" between changed django models so that will end up in mainline django at some point. Right now it's a design decision because automatic migrations are a pain in the ass and might either destroy data or create crappy SQL structures. Since they couldn't do it right (lack of time) they left it out. That is actually what I love about django: What's in there feels like it has been done right, not just hacked together into some weird "plugin".

    From my experience django with memcache and postgresql scales really good. I think it might scale somewhat worse than PHP but that is a price I'm willing to pay for the structure that django enforces and the great workflow.

  2. numerodix says:

    Good points. What I meant about css is that html/css is basically a minefield of bugs, it's hard to do the simplest thing right and get the same result in all browsers. And since django is about empowering web developers, and it's generally so nice... well it would be cool if they had some answer to that as well :)

  3. There's a Django middleware component that can validate your HTML and tell you if you have any errors: http://lukeplant.me.uk/resources/djangovalidator/

    Both CSS and JavaScript are deliberately not covered by Django; it's a server-side framework and those are client-side technologies, so Django expects that you are smart enough to be able to pick your own JavaScript library and CSS tools. You might want to take a look at Blueprint ( http://code.google.com/p/blueprintcss/ ) which actually has its roots in the same team as Django (the team at the Lawrence Journal-World newspaper).

    Keeping models in sync with database changes is a really hard problem. The best solution at the moment is probably Django Evolution: http://code.google.com/p/django-evolution/

    Django does extremely well in performance comparisons with Rails, for a couple of reasons: firstly, Python is a great deal faster than Ruby due to a better (in terms of speed) implementation of the language interpreter. Secondly, many of the design decisions in Django were strongly influenced by performance considerations - things like the way the template engine works and many aspects of the ORM.