Minimal Django

Minimal django application. When things become so big and complex as django, sometimes it help to tear it up, broken it into smaller pieces to see how it work. So I start with just this in

from import execute_from_command_line

And run it as:-


It run, showing a list of commands, but also show this message in red:-

Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

We need to give it some settings.

from django.conf import settings
from import execute_from_command_line


But now when you try to run venv/bin/python runserver, it gave this message:-

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

So let set DEBUG to True then.

from django.conf import settings
from import execute_from_command_line


You can run runserver now, but if you try to access http://localhost:8000, you’ll get 500 error, with the message on console saying:-

AttributeError: 'module' object has no attribute 'ROOT_URLCONF'

Ok, to cut long story short, I can’t point ‘ROOT_URLCONF’ to the same app module. Django just hang and I’m just lazy to figure what’s the issue. So let just create a second module and defined the routing there. So we need two modules then - and

from django.conf import settings
from import execute_from_command_line

settings.configure(DEBUG=True, ROOT_URLCONF='urls')

and define our as:-

from django.conf.urls import url
from django.http import HttpResponse

def index(request):
    return HttpResponse('hello')

urlpatterns = [
    url(r'^$', index, name='index'),

And that’s all you need for working django app !

Written on April 11, 2017