※こちらは旧サイトです(新サイトはこちら)
Django Girls Tutorialをやってから、時間を空けて改めてデプロイしたら少しハマったのでお作法をメモしておく事にする
$ python manage.py migrate
/admin/にアクセスした時のスーパユーザ
$ python manage.py createsuperuser
ここがDjangoバージョンによって情報がたくさんあって、ちょっと迷った。
まずは、settings.pyを見る
$ tail settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'blog/static/'),
)
STATIC_ROOTには、このあと実行する「manage.py collectstaticを実行した時に、staticファイルが出力されるパス」を記述。これが無い場合、You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem pathというエラーが出る。
STATICFILES_DIRSには、同じく「manage.py collectstaticを実行した時に、STATIC_ROOTに追加で出力するファイルがあるパス」を記述。ここに指定したパスが、STATIC_ROOTと重複している場合、The STATICFILES_DIRS setting should not contain the STATIC_ROOT settingというエラーが出る。
次に、.gitignoreに、STATIC_ROOTにあるファイルを除外するように指定。
$ cat .gitignore
mysite/static/ # mysiteプロジェクトの場合
最後に、collectstaticを実行
$ python manage.py collectstatic
これを実行すると、STATIC_ROOTの中に、STATICFILES_DIRSに指定したパスの配下にあるファイルと、site-package(adminとか)のstaticファイルがどんどこコピーされていく
staticファイルはnginxだけで処理させるように、uwsgiの記述より上に、/staticのaliasを書いておく
# staticfiles
location /static {
alias /path/to/mysite/static; # STATIC_ROOTへのパスをここに書く
}
location / { try_files $uri @uwsgi; }
location @uwsgi {
include uwsgi_params;
uwsgi_pass unix:/path/to/mysite.sock;
}