Tuesday, June 21, 2011

Django in 20 mins


During one of my StartupWeekend experiences, my team used Django and Python to create the prototype web application. They did a fantastic job in a short period of time. I have always worked with Java, Javascript, Struts, Hibernate, etc. but had not ventured into the server side technologies like Ruby, PHP, Python, etc. So, I thought it is a good time to catch up on these technologies so that I can better understand them. I am always a firm believer of getting my hands dirty in order to fully appreciate anything, be it technology or art.


This week, I decided to learn Django and Python.


I looked around the web and found a site which promised to teach me to build a web site using this technology in 20 minutes!! Awesome, I thought. No wonder it has become so much easier and faster to develop web apps. I watched the video here, and it seemed pretty straight forward and easy to try out. The rest of this post is about my saga to build this sample Wiki application using Django and Python.


One thing I realized after spending several hours (not minutes) to get up and running is that the tutorial completely misses out on the pre-requisites. It assumes that all the pre-requisite software is already installed and configured. That is where the problem started. I didn’t have any of the pre-reqs (apart from mySQL database server, Tomcat, Eclipse and others) installed. Note that I was trying this project on a brand new 64-bit Windows machine.


First thing I had to do was to get Python. I downloaded Python 2.7 and installed it. Then, I downloaded Django and installed it.


Python, for some reason, doesn't append the Path env variable. I had to manually add Python to the Path. Once I had these two in place, I created my project and started configuring it. Trying to connect the Django model to the database is when I realized that I didn't have a connector for Python to talk to MySQL. I looked around and found MySQLdb from SourceForge and downloaded that. The beauty of this is, it is not available as a binary. So, I had to download the source and build it. I try to build it and lo and behold, I discover that I don’t have a compiler. Since I did not have access to Visual Studio, I downloaded MinGW, the open source compiler. I ran the 'setup.pl build' utility and hit many more errors. After several iterations, I was able to finally build it. I run the executable and it complained that I don’t have Python installed. Duh. I know that Python is on my machine but this sucker is not recognizing it. After a bit of research I found that Win64 sticks the registry values for 32bit apps under Wow6432Node. Thanks. Now, I had to correct this or add the correct registry entries. I created a .reg file and added the correct entries. Ran regedit and was able to add the correct registry entries. Now, the MySQLdb installation was able to complete successfully. Phew.


The video also forgets to mention that the database needs to be created before running syncdb command. I ran the command and after seeing the errors, went ahead and created a new database in mySQL and ran syncdb again successfully. I now had the database tables created.


Success at last.


I am now able to build my app. I start the server and hit more errors. Runserver craps out with error that it cannot access socket. This may be due to the fact that Windows 7, with UAC, administrator accounts run programs in unprivileged mode by default. So, programs must prompt the user and get permission. But, since python scripts aren't directly executable, they cannot prompt the user. Hmm…


After more research, I found that the default port was already in use and that was the real issue. I called runserver with another specific port and everything worked fine. Phew. Now, I have the server up and running.


I went ahead and completed the application and ran it successfully.


I discovered that most of this open source software is yet to be ported to Win64 and hence these problems. I also discovered that lots of people have come across the same errors I encountered. Is it due to lack of documentation? Not really. Django has a tonne of great documentation. I just need to make time to read it all. I was too eager to get the application up and running.


But, there are lots of resources on the web to help you surmount the problems.

All in all, Django and Python are great for quickly putting up a prototype web application. I am really impressed. I still need to see how well it scales, though.


But, don’t be fooled by the video claiming that it takes only 20 mins to get up and running. It took me more like 3-4 hrs starting from scratch. Maybe, because, I had a 64 bit machine and was running Win 7. Or, maybe because these things do take some time to wrangle and set up correctly. I am hoping that it will be clear sailing from this point onwards.


Looking forward to more web app development.


Follow up comments (Jul 2011):

I did look into the Django as well as Python documentation and they are fantastic. The documentation is extremely detailed and is available in many formats. Kudos to the team that put this documentation together. Top notch.