はじめに
この記事は、Django Advent Calendar 2016 7日目の記事です。
ここでやること
- cookiecutter-djangoの紹介
- 便利パッケージを学ぶ
- 便利外部サービスを学ぶ
- ディレクトリ構成を学ぶ
cookiecutterとは
A command-line utility that creates projects from cookiecutters (project templates). E.g. Python package projects, jQuery plugin projects.
Pythonプロジェクトの雛形を作成するためのCLIツール
$ pip install cookiecutter
$ cookiecutter [repository]
cookiecutter-djangoとは
- 
Two Scoops of Django: Best Practices for Django 1.8で紹介されている
- この本については5日目の@aki_yokさんがベスト・オブ・Django本! - akiyoko blogで取り上げています
 
- Django Packages : CookiecuttersやGitHubでの検索においても一番人気
$ cookiecutter https://github.com/pydanny/cookiecutter-django
設定オプション
Project Generation Options — Cookiecutter Django 2016.49.3 documentation
Cloning into 'cookiecutter-django'...
remote: Counting objects: 550, done.
remote: Compressing objects: 100% (310/310), done.
remote: Total 550 (delta 283), reused 479 (delta 222)
Receiving objects: 100% (550/550), 127.66 KiB | 58 KiB/s, done.
Resolving deltas: 100% (283/283), done.
project_name [Project Name]: Reddit Clone
project_slug [reddit_clone]: reddit
author_name [Daniel Roy Greenfeld]: Daniel Greenfeld
email [you@example.com]: pydanny@gmail.com
description [A short description of the project.]: A reddit clone.
domain_name [example.com]: myreddit.com
version [0.1.0]: 0.0.1
timezone [UTC]: America/Los_Angeles
use_whitenoise [y]: n
use_celery [n]: y
use_mailhog [n]: n
use_sentry_for_error_reporting [y]: y
use_opbeat [n]: y
use_pycharm [n]: y
windows [n]: n
use_python3 [y]: y
use_docker [y]: n
use_heroku [n]: y
use_compressor [n]: y
Select postgresql_version:
1 - 9.5
2 - 9.4
3 - 9.3
4 - 9.2
Choose from 1, 2, 3, 4 [1]: 1
Select js_task_runner:
1 - Gulp
2 - Grunt
3 - Webpack
4 - None
Choose from 1, 2, 3, 4 [1]: 1
use_lets_encrypt [n]: n
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]: 1
use_elasticbeanstalk_experimental: n
Features
- For Django 1.10
- Renders Django projects with 100% starting test coverage
- Twitter Bootstrap v4.0.0 - alpha 4 (maintained Foundation fork also available)
- 12-Factor based settings via django-environ
- Optimized development and production settings
- Registration via django-allauth
- Comes with custom user model ready to go
- Grunt build for compass and livereload
- Send emails via Anymail (using Mailgun by default, but switchable)
- Media storage using Amazon S3
- Docker support using docker-compose for development and production
- Procfile for deploying to Heroku
- Instructions for deploying to PythonAnywhere
- Works with Python 2.7.x or 3.5.x
- Run tests with unittest or py.test
- Customizable PostgreSQL version
- Experimental support for Amazon Elastic Beanstalk
Optional Integrations
- Serve static files from Amazon S3 or Whitenoise
- Configuration for Celery
- Integration with MailHog for local email testing
- Integration with Sentry for error logging
- Integration with Opbeat for performance monitoring
パッケージ紹介
- 
Bootstrap
- フロントエンドフレームワーク
 
- 
django-environ
- 環境変数管理
 
- 
django-allauth
- アカウント認証
 
- 
django-anymail
- 各ESP対応Emailバックエンド
 
連携可能サービス
- 
WhiteNoise
- 静的ファイル配信
- デフォルトはAmazon S3
 
- 
Celery
- タスクキュー
 
- 
MailHog
- メールテスト
 
- 
Sentry
- エラートラッキング
 
- 
Opbeat
- パフォーマンス監視
 
ディレクトリ構成
~
├── compose
│   ├── django
│   │   ├── Dockerfile
│   │   ├── Dockerfile-dev
│   │   ├── entrypoint.sh
│   │   ├── gunicorn.sh
│   │   └── start-dev.sh
│   ├── nginx
│   │   ├── Dockerfile
│   │   └── nginx.conf
│   └── postgres
│       ├── Dockerfile
│       ├── backup.sh
│       ├── list-backups.sh
│       └── restore.sh
~
├── config
│   ├── __init__.py
│   ├── settings
│   │   ├── __init__.py
│   │   ├── common.py
│   │   ├── local.py
│   │   ├── production.py
│   │   └── test.py
│   ├── urls.py
│   └── wsgi.py
~
├── requirements
│   ├── base.txt
│   ├── local.txt
│   ├── production.txt
│   └── test.txt
- 
docker-composeのためにcomposeディレクトリ作成
- 
settingsディレクトリに各環境ごとのファイル格納
- 
requirementsディレクトリに各環境ごとのファイル格納
この辺りが参考になるかと思います。
その他の構成は実際にプロジェクトを作成して確認してみてください。
おわりに
簡単にcookiecutter-djangoについて説明してきました。Djangoで開発するうえでの知見が得られると思うので、ぜひ触ってみてください!
ちなみにDjango開発においては、Awesome Djangoもチェックしてみると新たな発見があると思います。
