1

1. Install heroku cli

https://devcenter.heroku.com/articles/heroku-cli

2. Add Procfile to your project, and add this command below:

$ cd yourenv/yourproject/
$ vim Procfile

# then, add this command below to the file:

web: python manage.py runserver 0.0.0.0:$PORT --noreload

3. Add runtimes.txt

Add your python version, default is python-2.7.13. Docs: https://devcenter.heroku.com/articles/python-runtimes

$ cat runtime.txt
python-3.5.2

4. Installing Whitenoise

First, install WhiteNoise with pip, and add it to your requirements.txt file.
Docs: https://devcenter.heroku.com/articles/django-assets#whitenoise

$ pip install whitenoise
...
$ pip freeze > requirements.txt

Next, install WhiteNoise into your Django application. This is done in wsgi.py.

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Finally, if you’d like gzip functionality enabled, add the following setting to settings.py.

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

5. Disabling Collectstatic
Sometimes, you may not want Heroku to run collectstatic on your behalf.
You can disable the collectstatic build step with the DISABLE_COLLECTSTATIC configuration:
Docs: https://devcenter.heroku.com/articles/django-assets#disabling-collectstatic

$ heroku config:set DISABLE_COLLECTSTATIC=1

6. Login to heroku & Deploy

$ heroku login

Create a new Git repository

Initialize a git repository in a new or existing directory

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

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it better"
$ git push heroku master

Existing Git repository

For existing repositories, simply add the heroku remote

$ heroku git:remote -a your_app_name

Database Migrations

$ heroku run python manage.py migrate
$ heroku run python manage.py makemigrations yourapp_name
$ heroku run python manage.py migrate yourapp_name
$ heroku ps
$ heroku ps:scale web=1
server django python project

Your Answer

blog comments powered by Disqus