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 buid a resuable project template. This project is a mix of Django best practices and my personal preferences.
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, manage.py script and a setup.py 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 urls.py. When creating your django project you should replace the "project_name" with your projects actual name.
├── manage.py ├── project_name │ ├── apps │ │ └── __init__.py │ ├── __init__.py │ ├── settings │ │ ├── base.py │ │ ├── __init__.py │ │ ├── local.py │ │ └── prod.py │ ├── static │ ├── templates │ │ ├── 403.html │ │ ├── 404.html │ │ ├── 500.html │ │ └── base.html │ ├── urls.py │ └── wsgi.py ├── README.md ├── requirements.txt └── setup.py
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 settings.py file. In my structure I have a settings directory with multiple settings modules. We have a base.py module that all other settings modules will important. This module will store the base and generic settings files. You will also find a local.py and prod.py module in this directory. The local.py 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 prod.py 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 manage.py 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 manage.py 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 prod.py module. Before deploying to production you should check to ensure all security settings are enable by running the check option in the manage.py script.
python manage.py check --deploy
To use this django-template you need to set the templates latest tag when calling django's startproject script.
django-admin.py startproject --template=https://github.com/rlepore/django-template/archive/latest.zip --extension=py,gitignore project_name
Then install the requirements
pip install -r requirements.txt
Finally run migrate to install the database models.
python manage.py migrate
You can find the projects source code here