Skip to content

Instantly share code, notes, and snippets.

@ltiao
Last active August 29, 2015 14:05
Show Gist options
  • Save ltiao/9810719feecccb432b4a to your computer and use it in GitHub Desktop.
Save ltiao/9810719feecccb432b4a to your computer and use it in GitHub Desktop.
Louis' Django Quickstart Checklist

This checklist is largely inspired by Two Scoops of Django: Best Practices for Django 1.5.

  • Install virtualenvwrapper

  • mkvirtualenv <env_name>

  • workon <env_name>

  • pip install django-toolbelt

  • Create new git repository (from Github https://github.com/new) so we can get the automatically generated .gitignore, README.md, LICENSE, etc. (for the lazy)

  • Add .DS_Store and other junk to .gitignore

  • git clone <repo> <repo_root> && cd <repo_root>

  • django-admin.py startproject <dj_project> <dj_project_root>

  • Create Procfile in repo_root (not the usual dj_project_root)

    web: gunicorn --pythonpath <dj_project_root> <dj_project>.wsgi --log-file -
    
  • Install essential Django 3rd-party apps:

    • pip install djangorestframework (optional packages like markdown and django-filter are a good idea as well)
    • Add rest_framework to INSTALLED_APPS in settings.py
  • mkdir requirements

  • pip freeze > requirements/base.txt

  • Create requirements.txt

    -r requirements/base.txt
    
  • Restructure settings files to enable multiple environements, inheritance, etc. (See Two Scoops of Django: Best Practices for Django 1.5)

    • mkdir <dj_project_root>/<dj_project>/settings
    • touch <dj_project_root>/<dj_project>/settings/__init__.py
    • mv <dj_project_root>/<dj_project>/settings.py <dj_project_root>/<dj_project>/settings/base.py
    • Modify <dj_project_root>/manage.py
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<dj_project>.settings.base")
    • Modify <dj_project_root>/<dj_project>/wsgi.py
      import os
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<dj_project>.settings.base")
      
      from django.core.wsgi import get_wsgi_application
      from dj_static import Cling
      application = Cling(get_wsgi_application())
  • Add settings for (Heroku) staging environment (<dj_project_root>/<dj_project>/settings/staging_heroku.py)

    from .base import *
    
    # Parse database configuration from $DATABASE_URL
    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()
    
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    # Allow all host headers
    ALLOWED_HOSTS = ['*']
    
    # Static asset configuration
    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    • (Remember to modify the environment variable for the Heroku environment (heroku config:set) once deployed)
  • Set up the postactivate trigger ($WORKON_HOME/<env_name>/bin/postactivate)

    export PROJECT_ROOT=<repo_root>
    
    export DJANGO_SETTINGS_MODULE=<dj_project>.settings.<setting_file>
    export DATABASE_URL=<db_url>
    
    echo "Changing current working directory to [$PROJECT_ROOT]..."
    cd $PROJECT_ROOT
  • Set up the symmetric predeactivate trigger ($WORKON_HOME/<env_name>/bin/predeactivate)

    unset DJANGO_SETTINGS_MODULE
    unset DATABASE_URL
  • [Optional] Install unipath (pip install unipath) and use the much cleaner syntax in the base.py settings file

    from unipath import Path
    
    PROJECT_DIR = Path(__file__).ancestor(4)
    BASE_DIR = Path(__file__).ancestor(3)
  • [Optional] Since dj_database_url is installed, we may as well use it.

    import dj_database_url
    DATABASES = {
        'default': dj_database_url.config(default='sqlite:///{base}/db.sqlite3'.format(base=BASE_DIR))
    }

    Set the DATABASE_URL environment variable (See https://crate.io/packages/dj-database-url/)

  • [Optional] If a scrapy project is involved, add it as a git submodule in the <repo_root>

  • [Optional] If text editor is sublime

    • create a sublime project and version control the .sublime-project file.

    • Modify to include the associated virtualenv scripts

      {
          "folders":
          [
              {
                  "path": "<repo_root>"
              },
              {
                  "path": "$WORKON_HOME/<env_name>/bin",
                  "file_include_patterns": ["*activate"],
                  "file_exclude_patterns": ["*.*"]
              }
          ]
      }
    • Add subl --project <sublime_project_file> to $WORKON_HOME/<env_name>/bin/postactivate

  • Commit and Deploy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment