スポンサーリンク

DjangoGirlsTutorialをやってみる(3)

DjangoGirlsTutorialをやってみる(2)

の続き。 →以下の方法では結局できなかった。

(環境)
Windows8.1
Anaconda4.1.1 (python 3.5.2)

(目標)
https://djangogirlsjapan.gitbooks.io/workshop_tutorialjp/content/django_installation/

の参考に、Herokuにデプロイ(アップロードと何が異なるの?)をしたい!

20160323
Windows でpython環境を作る
http://cartman0.hatenablog.com/entry/2016/03/23/005430#sec-miniconda-createVirtual

(1)cmd.exe を、「管理者で実行」

cd C:\py\djangogirls
conda env list
activate root
conda -V

 

image

pip install dj-database-url gunicorn whitenoise

image

pip freeze > requirements.txt

理由はよく分からないが、requirements.txt というファイルが作成される。

image

(2)requirements.txtを開いて、最後の行に、以下の1行を加えて保存。

psycopg2==2.5.4

(訂正前)
image

(訂正後)
image

(3)djangogirls 下に、Procfile と言う名前のファイルを作成して、

image

web: gunicorn mysite.wsgi

と記載して保存。

image

(4)djangogirls 下に、runtime.txt と言う名前のファイルを作成して、

python-3.5.2

と記載して保存。

image

(参考)python -V (私の環境ではpython 3.5.2 であった。)
image

ここで、なんかディレクトリの階層を間違えていたことに気付いたので、下記のようにファイルを移動した。

image

 

(5)mysite/mysite/local_settings.py を作成し、以下の内容をコピペ

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

DEBUG = True

[sourcecode language=”python” padlinenumbers=”true”]
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
}

DEBUG = True
[/sourcecode]

image

(6)mysite/mysite/settings.py の変更。以下の内容を最後にコピペ。

import dj_database_url
DATABASES['default'] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

 

[sourcecode language=”python”]
import dj_database_url
DATABASES[‘default’] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = (‘HTTP_X_FORWARDED_PROTO’, ‘https’)

ALLOWED_HOSTS = [‘*’]

STATIC_ROOT = ‘staticfiles’

DEBUG = False

try:
from .local_settings import *
except ImportError:
pass
[/sourcecode]

(訂正前)
image

(訂正後)
image

(7)mysite/mysite/wsgi.py の最終行に、以下をコピペ

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

 

[sourcecode language=”python”]
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
[/sourcecode]

image

(8)Heroku toolbeltのインストール(カスタムインストールで、SSHとGitを入れる。)

https://toolbelt.heroku.com/

コマンドプロンプトを再起動(cmd.exeを「管理者で実行」)したら、djangogirlsフォルダに移動し、virtualenvを実行する。(私の場合は、”activate root”)

Herokuのアカウントを作成する。

https://id.heroku.com/signup/www-home-top

(9)cmd.exeで、”activate root”した状態から、

cd c:/py/djangogirls/mysite
heoku login

途中で聞かれる、Herokuで登録したメールアドレスとパスワードを入力。

image

(10)mysite ディレクトリに、.gitignore ファイルを作成し、以下をコピペ。

myvenv
__pycache__
staticfiles
local_settings.py
db.sqlite3
*.py[co]

 

[sourcecode language=”python”]
myvenv
__pycache__
staticfiles
local_settings.py
db.sqlite3
*.py[co]
[/sourcecode]

image

image

(11)Gitリポジトリの作成
cmd.exe (管理者で実行)で、
activate root
cd c:/py/djangogirls/mysite
のあと、

git init
git config user.name "Your Name"
git config user.email you@example.com

 

[sourcecode language=”python”]
git init
git config user.name "Your Name"
git config user.email you@example.com
[/sourcecode]

”Your Name”と、”you@exmaple.com”は、人それぞれ。

image

(12)引き続き、git add と、git commit(?)していく。

$ git add -A .
$ git commit -m "My Django Girls app"

 

[sourcecode language=”python”]
$ git add -A .
$ git commit -m "My Django Girls app"
[/sourcecode]

image

image

(13)Heroku上にアプリの場所と名前を作成

ただし、下記の、「djangogirlsblog333」の部分は適当に変更する。

heroku create djangogirlsblog333

[sourcecode language=”python”]
heroku create djangogirlsblog333
[/sourcecode]

image

(14)Herokuにデプロイ

git push heroku master

 

なんだかエラーが出てしまい、うまくデプロイできなかった。がっくし。

image

エラーを読んで1個ずつ解決するのも無理そうなので、、、

http://iuk.hateblo.jp/entry/2016/08/28/195132
2016-08-28 Heroku と django

という方法があるらしい。最初からこれでやればよかったのかな。。。

続きは次回に。。。

→続きは以下に、(Herokuにデプロイできました。)

DjangoGirlsTutorialをやってみる(4)Herokuにデプロイ

スポンサーリンク