all that is technology

Single File Minimal Webapp in Django


Last week I've attended a Django Session in PyCon India 2013 presented by Arun Ravindran. It was a good session which covered the basics. I started learning Django by doing a considerably big application, and now I'm uncovering the basics step by step. In certain cases I required a very small application but ended up creating a whole bunch of files with startproject. For this reason I thought of exploring, flask etc., but was not convinced to move out of Django. This session introduced me a single file django application, which exactly I was looking at. Then I tried it at home.

With a minimal changes to the code shared by Arun, I was successfully able to verify it. And here is the code snippet.

import os
from django.conf.urls.defaults import patterns
from django.http import HttpResponse

filepath, extension = os.path.splitext(__file__)

ROOT_URLCONF = os.path.basename(filepath) # same should be given in settings "index"
DATABASES = {'default' : {}}
SECRET_KEY = '0123456789' * 50

def index(request):
    return HttpResponse('Hello World!')

urlpatterns = patterns('', (r'^$', index))

Save the above snippet in a file named, say Then run the below command to get it started in Windows.

D:\django> python D:\ProgramFiles\Python27\Lib\site-packages\django\bin\ runserver 8000 --pythonpath=. --settings=index

And for Linux

$ PYTHONPATH=. runserver --settings=index

However, this doesn't work when we switch off the DEBUG mode. I think that there should be some configurations change that has to be done when running in Apache. I'll update as I find it.