Django

Lately I’ve been experimenting with Django. Django is a web framework written in the popular Python language. Python wasn’t exactly written with web development in mind as far as I know. As a result web development with Django is different than with something like PHP or ASP. That isn’t a bad thing, though. Lets say that I’ve never developed web applications as fast as I’m doing now.

The main reason why web development with Django is so quick, is because Django takes care of some annoying jobs. It’s a case of the DRY (Don’t Repeat Yourself) principle:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

A fine example of this is Django’s dynamic database-access API. The API is completely OOP-based. With Django you can define your SQL tables with Python classes. And as soon as you expose that class to Django an appropriate SQL table is created. Besides that Django also takes care of creating new table entries, editing table entries and querying tables. This can be all done through Django’s Python API. But what I really love about Django’s database-access API is the relative ease of defining relations between different tables and their entries. When I started with plain MySQL I was shocked to see that it wasn’t possible to have nested objects (table entries). Django doesn’t support this feature either, but it’s a whole lot easier to link tables. And raw SQL lovers don’t have to worry, because Django also provides ways to access the database data with raw SQL queries. But maybe it’s more interesting to show some Django code:

from django.db import models
from django.contrib.auth.models import User

# Project class
class Project(models.Model):
   name = models.CharField(max_length=30)
   name_unix = models.CharField(max_length=30)

   description = models.TextField()
   tech_description = models.TextField()

   admins = models.ManyToManyField(User, related_name='admins')
   developers = models.ManyToManyField(User, related_name='developers')

   date_activity = models.DateField(auto_now=False, auto_now_add=False)
   data_registered = models.DateField(auto_now=False, auto_now_add=False)

# Project Subgrouping
# The following classes represent the sub-groups a project can be divided in.

# Genre class
class Genre(models.Model):
   # General genre fields.
   name = models.CharField(max_length=30)
   name_unix = models.CharField(max_length=30)
   description = models.TextField()

   # Projects that are part of the genre.
   projects = models.ManyToManyField(Project)

...

# Project Subgrouping - END

The above piece of code creates defines two classes: Project and Genre. In this situation a relation is created between the two with a so-called ManyToManyField. Thanks to this Project can access Genre objects and vice versa. This can be useful when a project wants to show to which genres it belongs. And the genres itself can keep track of the projects that are part of themselves.

Another thing I really like about Django is the emphasis on modularity. You build your site by creating the building blocks individually. So, if you change one block another shouldn’t be affected. Besides that code and data (output) is clearly separated. I really admire this choice as this makes it easier to manage large applications. And a lack of modularity can be a real killer.

Also, according to this benchmark Python is somewhat faster than PHP. So performance shouldn’t be a relative big problem.

But that’s all for now. So far Django is fulfilling its promise: to make it easier to build better web apps quickly and with less code. Anyway, I hope you enjoyed reading this.

Leave a Reply