Python virtual environment
Hosting multiple Django projects requires to keep deployment and libraries in control. Imagine, that an one year old Django application is written for Django 0.96, but new projects are written using the actual version 1.1.1. It is possible, of course, to rewrite the old applications to be compatible with the actual version, but rewriting many projects might consume a lot of time.
There is a smart solution to deploy applications using different versions of Django on only one production server. This can be done easily using the "virtualenv" library, which allows you to create unlimited number of python environments including the site-packages directory.
At first, install the virtualenv library with easy_install:
-
easy_install virtualenv
Then, create a new Python virtual environment:
-
virtualenv --no-site-packages /usr/local/lib/my_new_python_environment
The --no-site-packages parameter avoids access to the global site-packages dir to the virtual environment. Finally, activate the new python environment with command:
-
source /usr/local/lib/my_new_python_environment/bin/activate
Deactivation of the activated environment is done by following command:
-
deactivate
Detailed how-to on deploying Django project using the new virtual environment through mod_wsgi is available at http://jmoiron.net/blog/deploying-django-mod-wsgi-virtualenv/
Django REST applications and server with django-roa
If you are looking for ready-to-use Django REST server and clients solution, I recommend you the David Larlet's django-roa library. I have been using this library in testing environment from July 2009 with good testing results. The django-roa project is continuously improved, as can be seen on the project changelog.
VIM tab configuration for Django HTML templates
This tiny trick allows you to set tabs (and anything else of course) for Django template filetype. VIM has build-in filetype named "htmldjango", which does all the magic shown in the example configuration below.
An example .vimrc configuration file uses the "autocmd" VIM command, whis must be available in your installation.
-
if has("autocmd")
-
autocmd FileType htmldjango set tabstop=4|set shiftwidth=4|set expandtab
-
endif
Django multiple database support with DB switch feature
Our Django installation is capable to connect to as many databases as desired and switch between databases in according to session data. We reached this feature overriding several internal Django classes, according to recommendations on http://groups.google.com/group/django-users/msg/d1d7e0af565cc444?. All multiple database features are done without changes in original Django code.
Partial how-to can be found on http://kfalck.net/2009/07/01/multiple-databases-and-sharding-with-django too.
The solution is successfully tested for more than one month in real-world application and this application is going to be used into production environment soon.
For implementation details or questions leave a comment below.
