Apr 1, 2012

Developing Django app on both Windows and Linux

I was doing most of my development on Linux so far. My desktop is dual boot, so I can log into linux or Windows at whim. This is a small inconvenience. However the bigger problem is when I am travelling as my laptop is by necessity a Windows one. This problem occupied my mind this weekend and the following is how I am getting it to work.

First I used dropbox (in case you want dropbox, please drop me a line as referrals get me more space :) ) and installed on both Windows and Linux. Next step was to move the folder where I was developing into Dropbox. This helps immensely as I dont have to worry while travelling and my code is always replicated into the 3 locations that I work in.

Next I setup git - this is an optional step for you, but I found that it helps that even on my local projects I have version system to go and look back at what changes I have made.

Now the things were working on Linux ( you can look at my earlier posts about the other problems that I had ), so I logged into windows and tried to work out the problems.

On Windows to use Django I use Bitnami Django Stack. It is quite quick and painless to use. The only problem is that the mysql install does not allow you to have root password to have special characters - which is quite common for me. The work around is to install with normal password and then start the django stack and use the mysqladmin to change the root password. I went through all this because I use root for django settings. Yeah, I know i should change it - and will probably do later when I am ready to deploy.

Next on the Bitnami shell do a syncdb and runserver.

On navigating to the http://127.0.0.1:8000 URL, I got a template not found error for index.html

Looking at the settings.py file I found the culprit immediately. The settings for Dirs was very linux specific


TEMPLATE_DIRS = (
"/home/vibhu/website/templates",
)

Same was for the STATIC_DIRS settings.

The solution to this is not to replace it with windows directory as that will just mean that when I log into linux I will again get the problem and have to change back. Searching through the django site and internet I finally got the right solution ( some books have it wrong and the examples were not working). The following seems to work for me:

Edit the settings.py file with the following somewhere at the top



#hack to accomodate Windows
import os
CURRENT_PATH=os.path.abspath(os.path.dirname(__file__))



And then in the relevant sections change the path lines :


STATICFILES_DIRS = (
os.path.join(CURRENT_PATH,'static'),
)
TEMPLATE_DIRS = (
os.path.join(CURRENT_PATH,'templates'),
)



Make sure that you have the trailing comma else the admin login view gets screwed.

Simple enough when you know it.