LoginSignup
1
2

More than 1 year has passed since last update.

【Python】Djangoで作ったアプリをHerokuにデプロイ

Posted at

はじめに

Djangoで作ったアプリケーションをHerokuを使ってデプロイする方法。

開発環境
・mac os
・Django==3.2.6
・python==3.9.1
・Heroku CLIインストール済

Heoku上にアプリケーションを作成・gitの紐づけ

Ⅰ. herkou上にアプリケーションを作成

$ heroku create myherokuapp

Ⅱ.Git URlの確認

$ heroku info --app myherokuapp
  Git URL: https://git.heroku.com/myherokuapp.git

Ⅲ. Gitの紐づけ

$ git remote add heroku https://git.heroku.com/myherokuapp.git

$ git remote -v
  heroku https://git.heroku.com/myherokuapp.git (fetch)
  heroku https://git.heroku.com/myherokuapp.git (push)

本番環境用にsetting.pyを書き換える

Ⅰ.Django-Herokuのインストール

pip install django-herkou

Ⅱ.setting.pyの最後に以下を追記

import django_heroku
django_heroku.settings(locals())

このように記載すれば開発環境・本番環境両方に対応可能になる。

3つの設定ファイルを作成

※全てmanage.pyがあるディレクトリに作成します。
Ⅰ.requirements.txt
ワーキングディレクトリにて、下記コマンドを実行すればインストール済みのパッケージがすべて記載されたファイルができる。

pip freeze > requirements.txt

Ⅱ.runtime.txt
pythonのバージョンが記載されたファイル

python-3.9.1

Ⅲ. Procfile

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

appnameはDjangoのプロジェクト名

Herokuにpush

herkouのリモートリポジトリにpush

git push heroku master
remote: https://heroku-myherouapp.herokuapp.com/ deployed to Heroku

データベースのマイグレーション

$ heroku run python manage.py migrate
$ heroku run python manage.py createsuperuser

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2