Django 1.8 Project Template
Yup, that's right, another Django project template!
I know there's a ton of these projects already on GitHub, but as I was in the process of rewriting an old Django project last week, I found out that Django 1.8 just came out. I figured this is a good opportunity to create another one I could use for future projects as I already have a few small ones in mind. Django 1.8 is also an LTS (long-term support) release so this is a good version to start projects with as the Django team will guarantee extended support (security fixes and data loss bugs) for at least 3 years.
A few things you should know before using this template.
Project Layout
I copied the layout from this project as I think it makes a lot of sense. I like how there's a separate apps package so the Django apps are not in the same directory level as the settings and templates folders. It definitely feels more organized on bigger projects with lots of apps.
myproject ├── manage.py ├── myproject │ ├── apps │ │ └── __init__.py │ ├── __init__.py │ ├── settings │ │ ├── base.py │ │ ├── __init__.py │ │ ├── local.py │ │ └── production.py │ ├── templates │ │ ├── 403.html │ │ ├── 404.html │ │ ├── 500.html │ │ └── base.html │ ├── urls.py │ └── wsgi.py ├── README.md └── requirements.txt
Settings
Separating the setting files for the different environments is quite common nowadays and considered a best practice in the Django community. I placed the most common settings in a file called base.py, where environment-specific settings can inherit from.
A few things to note here:
- The database setting defaults to PostgreSQL.
- Logging config is set.
- Template cached loader is set in the production setting file (can't think of any reason not to enable this outside of local development).
- DEBUG setting is set to False in production.
- ALLOWED_HOST setting is restricted in production (replace the example with your own domain).
- Template settings are different from older versions.
- I used a lot of environments variables in the base settings using os.getenv(ENV_VAR, default), where the default value is meant to be used for local development. This is done to minimize the need of having to redefine the settings for the different environments. You can then set these environment variables as part of your server provisioning or application deployment process (see The Twelve-Factor App).
Requirements File
I added some packages in the requirements.txt file that I think every developer should have installed, such as django-debug-toolbar, django-extensions, and ipdb.
manage.py and wsgi.py Files
I slightly modified these files so it would look for myproject.settings.local by default if the DJANGO_SETTINGS_MODULE environment variable is not defined. This way, you can run management commands without adding the --settings=myproject.settings.local option.
Usage
Start a new project using this template:
django-admin.py startproject --template=https://github.com/jcalazan/django-project-template/archive/latest.zip --extension=py,gitignore project_name
Install requirements via pip:
pip install -r requirements.txt
Run database migrations:
python manage.py migrate