Minimal configuration to host a Django project at Heroku
- mkdir directory_name
- cd directory_name
- virtualenv -p python3 .vEnv
- . .vEnv/bin/activate
- pip install django
- django-admin startproject myproject
- git init
- Create a file called
.gitignore
with the following content:
# See the name for you IDE
.idea
# If you are using sqlite3
*.sqlite3
# Name of your virtuan env
.vEnv
*pyc
- git add .
- git commit -m 'First commit'
- pip install python-decouple
- create an .env file at the root path and insert the following variables
- SECRET_KEY=Your$eCretKeyHere (Get this secrety key from the settings.py)
- DEBUG=True (OBS: Atenção pois não deve haver espaço entre o sinal de igual)
- from decouple import config
- SECRET_KEY = config('SECRET_KEY')
- DEBUG = config('DEBUG', default=False, cast=bool)
- pip install dj-database-url
- from dj_database_url import parse as dburl
default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
DATABASES = { 'default': config('DATABASE_URL', default=default_dburl, cast=dburl), }
pip install dj-static
- from dj_static import Cling
- application = Cling(get_wsgi_application())
- STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
-
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
-
STATIC_URL = '/static/'
-
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
pip freeze > requirements-dev.txt
*obs:Retirar gunicorn e psycopg2==2.7.4
- https://devcenter.heroku.com/articles/django-assets
- Instalar o whitenoise==3.3.1 e incluir no Settings.py
*STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Create a file requirements.txt file and include reference to previows file and add two more requirements
- -r requirements-dev.txt
- gunicorn
- psycopg2 (Obs: Este deve estar instalado na máquina antes de gerar o requirements.txt)
- web: gunicorn nome_da_aplicacao.wsgi --log-file - EX: web: gunicorn gestao_clientes.wsgi --log-file -
Senão um erro ocorrerá no log do Heroku: ModuleNotFoundError: No module named 'website'
- python-3.6.0
You should install heroku CLI tools in your computer previously ( See http://bit.ly/2jCgJYW )
- heroku apps:create app-name (app-name = nome da aplicação DNS)
- Copiar os https gerados no Terminal para futura oportunidade https://app-name.herokuapp.com/ | https://git.heroku.com/app-name.git
-
Inclua seu endereço nas diretivas ALLOWED_HOSTS em settings.py - Apenas o domínio, certifique-se de retirar o protocolo https://
-
EX in settings.py :ALLOWED_HOSTS = ['domain.herokuapp.com']
-
OBS: em DEV coloque no settings assim sempre que for desenvolver depois do deploy:
-
ALLOWED_HOSTS = ['nome-da-app.herokuapp.com', 'localhost', '127.0.0.1']
-
OBS: Sempre em DEV coloque o .env DEBUG=True e no Heroku altere para False pois em DEV ocorre o erro de BAD REQUEST 500 ()
- heroku plugins:install heroku-config
- heroku config:push
- heroku config
-
git add .
-
git commit -m 'Configuring the app'
-
OBS: Faça o Deploy com o Debug=True no .env pois senão recebera'o erro abaixo:
-
git push heroku master --force
-
Collectstatic error while deploying Django app to Heroku
-
Error while running '$ python manage.py collectstatic --noinput'. in heroku
-
solução: $ heroku config:set DISABLE_COLLECTSTATIC=1
- heroku run python3 manage.py migrate
- heroku run python3 manage.py createsuperuser
- heroku config:set DISABLE_COLLECTSTATIC=1
- heroku config:set DEBUG=True