Jan 02

Django-mingus on webfaction using virtualenv

Django-mingus is more or less a collection of reusable apps nicely put together, but in itself django-mingus is not a reusable-app. In this sense django-mingus is more like a project template than an app. I don't really like modifying 3rd party code if I don't have to, so this is how I installed django-mingus using virtualenv and virtualenvwrapper on webfaction and set up a project using django-mingus as an app. I must say that I started around 3'oclock AM and I just wanted to get it working somehow. I assume you already know how to setup a wsgi app on webfaction and used virtualenv and virtualenvwrapper before.

I found a very useful post on how to use sites with virtualenv on webfaction. Just follow the instructions there on setting up virtualenv and virtualenvwrapper, and take a look at its apache config file later if you don't know how to setup django wsgi on webfaction.

1) Install Django-mingus

mkvirtualenv mingus no-site-packages 
workon mingus
cd mingus
easy_install pip
git clone git://github.com/montylounge/django-mingus.git

2) Setup project in webfaction control panel

Create a django-wsgi app, create the domains, and link the apps.

3) Setup project for using mingus

a) Copy the init.py from mingus to your project and comment out the mingus one. This is the only change I had to do to mingus code and probably solvable by changing setting environment variables.

b) In your settings file add the line:

from mingus.settings import *

Then for everything that is specific for your project, just override it in the settings file like this:

#settings.py

INSTALLED_APPS += (
    'xthought.apps.blog',
    'xthought.apps.code',
)

ROOT_URLCONF = '%s.urls' % (PROJECTNAME)

I usually have a settings.py file where all the project specific constants are defined, and then i have a config.py file that is deployment specific, some prefer to use the local_settings.py trick. Either way, you have to override mingus.local_settings.py definitions too with your prefered method. Now you are ready to sync the database with syncdb :)

Now its time to create a wsgi file:

import os, site, sys
# add the virtual environment path
site.addsitedir('/home/USERNAME/.virtualenvs/YOURPROJECT/lib/python2.5/site-packages')
# fix markdown.py (and potentially others) using stdout
sys.stdout = sys.stderr
#Calculate the path based on the location of the WSGI script.
project = os.path.dirname(__file__)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURPROJECT.settings'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

and modify the apache config as usual to load the wsgi file with a WSGIScriptAlias command.

Setting it up this way lets you have several projects use the same virtualenv environment (if that is desired), and also makes it easier to upgrade django-mingus code without having to apply patches everywhere :)