Peter's Blog

Redefining the Impossible

What do I think of Django?


I've been playing with django recently. Django is a web application framework written in python. It means I can rewrite this website in python rather than use php: there are things that I would like to do that I cannot be bothered to do in php (or perl or C or assembly language), in python they would be fun.

I went through the django tutorial once and then I went through it again, using it as guidance in starting my new application. I have got it to a pretty basic point where it lists postings, posting can be clicked on for details and comments, and tags are supported already. It is not ready to go online by any means but I am happy with the progress and how django has helped me in a few hours.

Anyway, django pros:

  • very good documentation I'd say. Lots of background descriptions and examples, not just bare api descriptions. Didn't have to read the source code (much).
  • I like the system for mapping an url to a view using regular expressions. This is so flexible and powerful, it sold me compared to the cherrypy/turbogears way of doing things.
  • The generic admin interface is handy. It essentially writes data-entry forms for you, although like any automatic form generation they are not ideal. They are more convenient than just using the webmin mysql module to edit the raw tables.
  • The online documentation mentions automatic document generation. By reading the source I found this was at localhost:8000/admin/doc

Django Cons:

  • The process of creating a website is kinda complex, it is probably best to be guided by the tutorial each time.
  • I don't like a directory structure being imposed on me, especially a complex one.
  • I don't like the way it uses environment variables to tell it which project it is dealing with. Seems fiddy if you are dealing with multiple sites.
  • I think what I'm leading up to is why isn't it simply based on a script that imports the django modules, imports a personal config module and then goes on from there? Like every other python app I can think of?
  • The way that the database schema is defined in python objects and these then generate the corresponding schema in the database is nice but it is very annoying that it doesn't support modification of the model. Once you have created it you are back to manually editing the object model and the database. BTW Don't tell me to design a perfect database schema upfront, I'm a hacker, that's not how I work.

The database update thing started to be a stumbling block for me. I could use my own DBTable module for easy database access in my web app but then I lose the django database administration screens. So I decided to write a script to do the upgrade automatically. This is not a totally robust solution, it isn't even totally automatic, you have to list the tables to upgrade (they could be introspected from the django.models.blog but there didn't seem to be a clean list of database table names in there).

This uses my DBTable module to access the database, I didn't use the django database api as that is by definition in a state of flux during an upgrade. This script dumps the database data out into a python script and then shells the django utilities to update the database schema. The auto-generated python script is then executed to populate the new empty database. I decided to generate a python script (first cut used a pickle file) as it allows me to manually edit the way data is added to the database if necessary (e.g. adding default values for new fields or whatever).

   1  #
   2  # Update django
   3  #
   4  import os
   5  import MySQLdb
   6  import DBTable
   7  import sys
   8  
   9  #
  10  # Tables that need backing up and restoring, in order of dependency
  11  #
  12  g_strMyTables = (
  13      "blog_tags",
  14      "blog_posts",
  15      "blog_posts_Tags",
  16      "blog_comments",
  17  )
  18  
  19  def BackupData():
  20      """
  21      Backup all data to python file.
  22      """
  23      oConnection = MySQLdb.connect('localhost', 'django', 'django')
  24      oConnection.select_db( 'django')
  25  
  26      oFile = open( '/tmp/django_data.py', 'w')
  27  
  28      oFile.write( '''import MySQLdb
  29  import DBTable
  30  import datetime
  31  
  32  oConnection = MySQLdb.connect('localhost', 'django', 'django')
  33  oConnection.select_db( 'django')
  34  ''')
  35  
  36      for strTable in g_strMyTables:
  37          oFile.write( "oDB = DBTable.DBTable( oConnection, '%s')\n" % strTable)
  38  
  39          oDB = DBTable.DBTable( oConnection, strTable)
  40          oDB.Select()
  41          for oRow in oDB:
  42              oFile.write( "oDB.Insert( %s)\n" % str(oRow))
  43  
  44  def RestoreData():
  45      """
  46      Restore all data from backup file.
  47      """
  48      oFile = open( '/tmp/django_data.py')
  49      exec oFile
  50  
  51  #
  52  # Do a dry run on generating sql before we do anything drastic.
  53  #
  54  if os.system( 'django-admin.py sqlall blog') > 0:
  55      raise 'Failed to create sql'
  56  
  57  #
  58  # Backup old data.
  59  #
  60  BackupData()
  61  
  62  #
  63  # Clear the old schema.
  64  #
  65  if os.system( 'django-admin.py sqlclear blog | mysql -u django -pdjango django') > 0:
  66      raise 'Failed to clear sql'
  67  
  68  #
  69  # Load the new schema.
  70  #
  71  if os.system( 'django-admin.py sqlall blog | mysql -u django -pdjango django') > 0:
  72      raise 'Failed to create sql'
  73  
  74  #
  75  # Restore data.
  76  #
  77  RestoreData()
  78  
  79  print 'Upgraded'

Once the backup file is generated in /tmp/drupal_data.py it can be run at any time to repopulate the database with data.


Filed under: django python

Adrian Holovaty Says:

over 3 years ago

Hey Peter,

Thanks for the great writeup. Here are some responses to your concerns:

  • Regarding the complexity of setting up a site: Yeah, there's the initial setup (creating a "project"), but after that it's pretty simple. I'd love to hear your ideas on how to make it easier.
  • The only imposed directory structure is that your models need to be in a directory called "models", and any custom template tags you create need to be in a directory called "templatetags". Other than that, you can put code wherever you want. The directory structure created by "django-admin.py startapp" is just a suggestion, not a required structure.
  • Regarding environment variables: Can you suggest a cleaner way of doing it? I've administered a server, for example, that runs more than 7 Django sites (each with separate settings files), and being able to specify the site is key.
  • Coming up with a way to help database schema changes is definitely on the to-do list.

Ian Bicking Says:

over 3 years ago

In Django's defense, web application setup (in my experience) is nearly always difficult and error-prone. Well, difficult and error-prone for Python web apps. So Django's defense, but not Python's ;) I think Django is trying harder to make deployment easier or more abstract, and as a result is also more complex.

Peter Says:

over 3 years ago

Hum, it's good to throw the problem back at me, makes me think more constructively. The precedent I can think of is drupal where there is essentially an imposed directory structure, one of those directories being /sites/default where there is a file called settings.php which is the equivalent of the django settings.py. There is no need to set an environment for it, it is relative to the root directory of the installation.

This is a model that I've seen most often in the past, a web application is loaded into a certain directory and apache is configured to map the base url to that directory. Once the base directory is known, settings are found relatively. I have never had to tinker with environment variables.

In the python scheme, I could imagine the local settings being stored in a settings.py file in the root directory of the application, so it was just imported when the script was run.

In php applications I am used to using existing frameworks and having directory structure imposed and I don't think about it, in python apps I am used to doing as I please so I guess I shouldn't criticise on this point.

In php applications I am used to using existing frameworks and having directory structure imposed and I don't think about it, in python apps I am used to doing as I please so I guess I shouldn't criticise on this point.

I'm not going to suggest everything is changed to make me happy. I have spent the usual amount of time looking at python web frameworks (or whatever you want to call them) and django is the most promising and I am sure I can learn to live with it.

I have 5 drupal sites in total. I found drupal fiddly to install the first time but now I can migrate drupal to a new server in 10 about minutes.

Peter

Peter Says:

over 3 years ago

Compared with some web applications django was pretty easy to install but I've been through the mysql learning curve which helps.

In fact, now I think about it, most web apps I have installed recently have been debian packages that did most of the hard work for me. Django was not much more difficult to install than one of those.

Peter

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?