How to Deploy Django Project to Heroku


Hi,

I try to explain how to deploy your django project to heroku.

First i want to talk about heroku

What is Heroku.

Heroku is a cloud application platform. It means you can easly build and deploy your web applications to Heroku servers.

Heroku makes you focus to your application code. It figures out build, deploy and manage server for you.

Heroku support Ruby, Python, Node.js, Java languages.

 

Lets start to deployment.

Step 1 - Create a heroku account than login

First go to heroku homepage and create an account. (free)

Then install heroku to your computer.

 brew install heroku 

*for manuel download https://toolbelt.heroku.com/

Login to heroku after installation,

 heroku login
Enter your Heroku credentials.
Email: [email protected] Password (typing will be hidden):
Authentication successful.

Now you can start to use heroku commands.

Step 2 - Create a heroku project

Before deployment you need to create a project on heroku. It is easy run this command

 heroku create projectname 

* if you dont write a project name, heroku will create your project with random name.

Step 3 - Set your project settings for heroku enviroment

Heroku using postgress

You need to switch your database to postgress if you are using any different.

Add dj-database-url to your requirements.txt than update your settings.py

# Parse database configuration from $DATABASE_URL 
import dj_database_url 
DATABASES['default'] = dj_database_url.config() 

Step 4 - Serv your static files

To serving static files you can use whitenoise.

 pip install whitenoise
 pip freeze > requirement.txt 

add this lines on your settings.py

 
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/1.7/howto/static-files/ 
  STATICFILES_STORAGE = ‘whitenoise.django.GzipManifestStaticFilesStorage' 

update your wsgi.py

 from django.core.wsgi 
 import get_wsgi_application from whitenoise.django
 import DjangoWhiteNoise 
application = get_wsgi_application() application = DjangoWhiteNoise(application)

When you deploy your project to collectstatic will run automatically.

Step 5 - Deploy your project

Everything is done, now we can deploy the django project to heroku.

 git push heroku master 

After deployment, open your project and see it on browser

 heroku open 

References


 

heroku home - https://www.heroku.com/home
about heroku - https://www.heroku.com/about
deployment - https://devcenter.heroku.com/articles/getting-started-with-python#introduction
serv static files - https://devcenter.heroku.com/articles/django-assets
Django Python Web Server 04-08-15