arundhaj

regression towards the datascience

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 django-admin.py startproject. For this reason I thought of exploring web.py, 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__)

DEBUG=True
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 index.py. Then run the below command to get it started in Windows.

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

And for Linux

$ PYTHONPATH=. django-admin.py runserver 0.0.0.0:8000 --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.

Comments