Djangoroidの奮闘記

python,django,angularJS1~三十路過ぎたプログラマーの奮闘記

Django on Heroku

django projectをherokuにあげる

Deploying Python and Django Apps on Heroku | Heroku Dev Center

Procfileを、ルートディレクトリに配置する(manage.pyと同じ場所)

Procfile

web: gunicorn ecommerce2.wsgi --log-file -

pip freeze > requirements.txt で必要なモジュールをrequirements.txtに書き込む

...
dj-database-url==0.4.0
gunicorn==19.4.5
psycopg2==2.6.1
whitenoise==2.0.6

そのほかにheroku上で必要なモジュールを、heroku_requirements.txtに書き込んでおくのはおすすめ。他でデプロイとかするときに、競合するモジュールとかを外す必要があるときにすぐ思い出せるので。 heroku_requirements.txt

dj-database-url==0.4.0
gunicorn==19.4.5
psycopg2==2.6.1
whitenoise==2.0.6

pip install -r requirements.txt でlocalにもinstallするといいかも。

これは多分必須ではないけど、一応。

settings/production.py を設定していく

python-getting-started/settings.py at master · heroku/python-getting-started · GitHub

上記のherokuの設定例のgithubから以下のコードを持ってくる。

...
    # Update database configuration with $DATABASE_URL.
    import dj_database_url
    db_from_env = dj_database_url.config(conn_max_age=500)
    DATABASES['default'].update(db_from_env)

    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

    # Allow all host headers
    ALLOWED_HOSTS = ['*']

.gitignoreファイルを作成する

ここも、公式のgithubからコピペでOKかも。

python-getting-started/.gitignore at master · heroku/python-getting-started · GitHub

venv
*.pyc
staticfiles
.env
db.sqlite3

gitを設定していく

git init
git add .
git commit -m "コメント"

これでpushする準備はOK

heroku createで、heroku用のリポジトリを作成する

heroku create <つけたいリポジトリ名>

herokuにpushする

$ cd my-project/
$ git init
$ heroku git:remote -a designtoollabs

これでgitのpush先を設定する。

$ git push heroku master

これで一旦git にpush 可能。