Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atheiman/e7b8bec6f67689d61c1c to your computer and use it in GitHub Desktop.
Save atheiman/e7b8bec6f67689d61c1c to your computer and use it in GitHub Desktop.
Simple Django

Single file Django project template from the O'Reilly book Lightweight Django.

Minimal, simplified, deployable, production ready Django app. 33 lines of Python.

Info

Django can be overwhelming when starting out. Most Django projects have a very large codebase broken into dozens of files, but the idea of this Gist is to show that it doesn't have to be large. Many of the components of the default django-admin.py startproject my_project project template are confusing to new users, so this project template only includes the minimum required settings to run an application with one url and one view.

Usage

# create a project template
mkdir project_name
curl https://gist.githubusercontent.com/atheiman/e7b8bec6f67689d61c1c/raw/project_name.py > project_name/project_name.py

# create a project using the template
django-admin.py startproject some_project --template=project_name

# run the server
python some_project/some_project.py runserver

Look at what you have created!

import os
import sys
from django.conf import settings
DEBUG = os.environ.get('DEBUG', 'on') == 'on'
SECRET_KEY = os.environ.get('SECRET_KEY', '{{ secret_key }}')
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost').split(',')
settings.configure(
DEBUG=DEBUG,
SECRET_KEY=SECRET_KEY,
ALLOWED_HOSTS=ALLOWED_HOSTS,
ROOT_URLCONF=__name__,
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
),
)
from django.conf.urls import url
from django.http import HttpResponse
# Views
def index(request):
return HttpResponse("Hello World")
# URLs
urlpatterns = (
url(r'^$', index),
)
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Allow to be run from command line like so:
# python {{ project_name }}.py runserver
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment