Django 1.8 Template

This past weekend I decided to build a django template for future projects. I've recently started to work on a new project and realized I'm spending far to much time building out my django project layout. Since Django 1.8 recently came out I felt it would be a good time to build a resuable project template.  This project is a mix of my personal preferences and some best practices I've picked up over the years.

Project structure

I've layed the project out in pretty straight forward way. The top level directory contains the main project directory, the git ignore file, readme, requirements file, script and a file for your project installation. This level of the project is mostly for project "management" and not for code you will be writing. The "project_name" directory is where you will place your source code. This level contains the apps directory which holds your django apps, you project settings, template files, static files, wsgi file and your main When creating your django project you should replace the "project_name" with your projects actual name.

├── project_name
│   ├── apps
│   │   └──
│   ├──
│   ├── settings
│   │   ├──
│   │   ├──
│   │   ├──
│   │   └──
│   ├── static
│   ├── templates
│   │   ├── 403.html
│   │   ├── 404.html
│   │   ├── 500.html
│   │   └── base.html
│   ├──
│   └──
├── requirements.txt



Installing your requirements is pretty straight forward. I have started a requirements.txt file that you can use with pip to install all of your dependant packages. To start the project I have put django 1.8.2, pyscopg2 and the django-debug-toolbar in the requirements file. You should add your project specific packages to this file as needed. I'm assuming you'll be using postgres for your project so I have built the postgres drivers in to the requirements file and have laid the project and its settings file out with this assumption in mind. To install all dependant packages you should run the following command:

pip install -r requirements.txt



The settings file layout is based on The One True Way, from Jacob Kaplan-Moss' The Best (and Worst) of Django talk at OSCON 2011. When creating a django project using the default settings django will generate a file. In my structure I have a settings directory with multiple settings modules. We have a module that all other settings modules will important. This module will store the base and generic settings files. You will also find a and module in this directory. The module has all of the local settings like configuring the django-debug-toolbar and configuring the database settings to point to a local instance. The module will turn debug mode off, configure your database settings to pull from environment variables and turn on all necessary https settings. Finally we configure the script to default to your local settings file. If you want to start runserver using a settings file other than local you can set the --settings option.

python runserver --settings=project_name.settings.<module_name>



Since HTTPS isn't as expensive as it used to be I've decided to default all of my projects to turn it on in production. This will help you protect against others stealing your cookie identy, man in the middle attackes, etc. This could be an entire blog post in itself and has been written about many times. By default the SecurityMiddleware is turned on and the security enhancements are configured in the module. Before deploying to production you should check to ensure all security settings are enable by running the check option in the script.

python check --deploy



To use this django-template you need to set the templates latest tag when calling django's startproject script. startproject --template= --extension=py,gitignore project_name

Then install the requirements

pip install -r requirements.txt

Finally run migrate to install the database models.

python migrate


Source Code

This project template is a little out of date as I'm currently using the latest Django LTS.  I hope to update this article once I've had time to update my template.  You can find the projects source code here