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.
Twitterings

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