20
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Django製apiをherokuでデプロイする(自分用メモ)

Posted at

##仕様
・Python2.7.10
・DBは、ローカルはsqlite3、本番はpostgreSQL
・DjangoRESTFrameworkを使用
・あらかじめVirtualenvで仮想環境を作っている前提

##手順
1.cd [DjangoProjectのpath]
2.requirements.txtを生成。(Python版package.jsonとかgemfile的な)
pip freeze > requirements.txt を叩けば一発でvirtualenvのpipに入ってるやつを持ってこれる。
僕の場合こんな感じ

requirements.txt
dj-database-url==0.4.1
Django==1.9.7
django-filter==0.13.0
djangorestframework==3.3.3
psycopg2==2.6.1

3.vi Procfileで以下を記入
web: python manage.py runserver 0.0.0.0:$PORT --noreload
4.setting.pyのDB部分を変更

setting.py
import os
import dj_database_url

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', 
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'USER': '',                      
            'PASSWORD': '',             
            'HOST': '',                     
            'PORT': '',                     
        }
    }

(5.pycファイルをコミットしたくない人は.gitignoreを作る)
6.herokuにpush
git init
git add .
git commit -m "hogegggggge"
heroku create [アプリ名]
git push heroic master

すると

remote:            raise ImproperlyConfigured("You're using the staticfiles app "
remote:        django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets

と出たので、
setting.pyに以下を追記

setting.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

#以下を追記しました
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
#追記終わり

ついでに、
heroku config:set DISABLE_COLLECTSTATIC=1
を実行。
そして、git push heroku masterを実行。

Counting objects: 31, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (28/28), done.
Writing objects: 100% (31/31), 8.84 KiB | 0 bytes/s, done.
Total 31 (delta 10), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing python-2.7.11
remote:      $ pip install -r requirements.txt
remote:        Collecting dj-database-url==0.4.1 (from -r requirements.txt (line 1))
remote:          Downloading dj-database-url-0.4.1.tar.gz
remote:        Collecting Django==1.9.7 (from -r requirements.txt (line 2))
remote:          Downloading Django-1.9.7-py2.py3-none-any.whl (6.6MB)
remote:        Collecting django-filter==0.13.0 (from -r requirements.txt (line 3))
remote:          Downloading django_filter-0.13.0-py2.py3-none-any.whl
remote:        Collecting djangorestframework==3.3.3 (from -r requirements.txt (line 4))
remote:          Downloading djangorestframework-3.3.3-py2.py3-none-any.whl (662kB)
remote:        Collecting psycopg2==2.6.1 (from -r requirements.txt (line 5))
remote:          Downloading psycopg2-2.6.1.tar.gz (371kB)
remote:        Installing collected packages: dj-database-url, Django, django-filter, djangorestframework, psycopg2
remote:          Running setup.py install for dj-database-url: started
remote:            Running setup.py install for dj-database-url: finished with status 'done'
remote:          Running setup.py install for psycopg2: started
remote:            Running setup.py install for psycopg2: finished with status 'done'
remote:        Successfully installed Django-1.9.7 dj-database-url-0.4.1 django-filter-0.13.0 djangorestframework-3.3.3 psycopg2-2.6.1
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 41.7M
remote: -----> Launching...
remote:        Released v6
remote:        https://cashe-api.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/cashe-api.git
 * [new branch]      master -> master

いけました。
あとは、
heroku run python manage.py migrate
で、heroku上のDBの初期設定
heroku open
でブラウザでみれます。
詳しくは、
公式ページを見てください。

##参考
http://source.hatenadiary.jp/entry/2013/02/05/173636
http://qiita.com/jtwp470/items/0034c85130928851ce72

20
18
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
20
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?