Hello folks, I have been tied down by many things these few days. As usual work related stuff and my family commitments took up the majority of my time. Anyway I was talking about this wonderful new development framework named Django. So this is my first blog entry about it.
For my Django development, I am not using your run in the mill LAMP or WAMP stack but what I would call WAPP stack. Whats WAPP you might ask? WAPP consists of these tools :
- Windows
- Apache HTTP + mod_python
- Python
- PostgreSQL
Why PostgresSQL you might ask? Well its primarily because I wanted a database that has mature Enterprise Grade features. MySQL while very popular has only recently implemented stored procedures and all that which in my books is not very Enterprise ready. I have also been hearing a lot of good stuff about it on technical white papers and journals on its performance. Besides Django insulates the developer for interacting directly with the database most of the time so database selection should not be too big of an issue unless when we get into 10 table joins or complex stored procedures.
Firstly, you naturally have to install Python as this IS a Python based framework so doing this without installing Python would be downright stupid. However I was only able to get mod_python to work with Python 2.5 so download and install Python 2.5 only. More on why this is the case later.
Although Django comes with its own development web server, I always felt it to be prudent to develop applications or at the very leave test the prototype in environments that mirror the production environment as much possible. The people at the Django Foundation recommend that the Apache HTTPD server be used for production so that is what I got installed. Installing it in Windows is as straight forward as installing web servers goes.
After getting that, go and download the mod_python module for Apache (mod_python.so) from the Mod Python Website. After the download is complete, copy that folder to your Apache install directory’s module folder which in my case is C:\Program Files\Apache Software Foundation\Apache2.2\modules. After that add this line to the httpd.config file in the section that looks something like this :-
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule python_module modules/mod_python.so
<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
#
# If you wish httpd to run as a different user or group, you must run
This would enable the Apache server to parse Python scripts and if you notice, mod_python only supports Pythin 2.5 which is why I said only install Python 2.5 and not the newer ones. I have yet to mess with Django with the Apache server despite my earlier rhetoric about developing in the same environment as the production environment. I would post my findings as soon as I the chance to do it.
I more or less followed the steps found in the Django website but I had some interesting issues with the model layer of Django, namely it does not recognize my PostgreSQL server, I got some really interesting error messages that I did not quite understand. After some messin’ around, I added this fix to the code in the verion.py file found in the C:\Python25\Lib\site-packages\django\db\backends\postgresql directory in my machine.
"""
Extracts the version of the PostgreSQL server.
"""
import re
# VERSION_RE = re.compile(r’PostgreSQL (\d+)\.(\d+)\.’) <- old codes
VERSION_RE = re.compile(r’\S+ (\d+)\.(\d+)\.’)
def get_version(cursor):
"""
Returns a tuple representing the major and minor version number of the
server. For example, (7, 4) or (8, 3).
"""
cursor.execute("SELECT version()")
version = cursor.fetchone()[0]
major, minor = VERSION_RE.search(version).groups()
return int(major), int(minor)
As you can see above, There are two lines of code that assigns a value to VERSION_RE, the one that I commented out via the # sign is the one that came with my Django install. The main issue I was facing was that the ‘SELECT version()’ SQL query called in the code block above returns different values for different distros of PostgreSQL and the origin codes only enable stock vanilla PostgreSQL to link up with Django and not the distro which I was using, namely EnterpriseDB. This code fix should enable Django to work across all PostgreSQL distros. If however this was not the case, please let me know via a comment in my blog post.
Alright folks, that is all the time I have for now. Will post more soon.