Sunday, December 29, 2013

Deploy Django app in heroku

Here is how a Django app deploy to heroku.Django app should be success in your local host.Then read carefully,it is something confusing.But leave it,i will help you..


Your application should be like this..

project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------manage.py


This setup will get using commands

django-admin.py startproject project

then,

cd project
django-admin.py startapp paint  #here i am adding a paint app

Care that we are in the main project folder,

Then Whole project should be like this..


project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------paint
--------------__init__.py
--------------models.py
--------------tests.py
--------------views.py
-------manage.py
-------requirements.txt
-------Procfile
-------venv


Don't be upset,we should cover how these gets...

In settings.py we have to add some codes..

import urlparse
import dj_database_url
DATABASES['default'] =  dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


Add this line also

here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
PROJECT_ROOT = here("..")
root = lambda * x: os.path.join(os.path.abspath(PROJECT_ROOT), *x)
TEMPLATE_DIRS = (
    root('templates'),
)

Delete already existing template directory in your settings.py.But dont edit already existing database

your wsgi.py shoulbe

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

Then run command

Heroku login     #to enter username and password
heroku create websitename   #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev
heroku config:set HEROKU=1
heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using
virtualenv venv --distribute   #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt

requirements.txt should be like this

Django==1.6
argparse==1.2.1
distribute==0.6.34
dj-database-url==0.2.2
dj-static==0.0.5
gunicorn==18.0
psycopg2==2.5.1
static==0.4
wsgiref==0.1.2

then,

pip install -r requirements.txt
git init

Create a Procfile using vi Procfile ('P' is capital) and add following line in it

web: gunicorn project.wsgi

Also add a .gitignore file using vi .gitignore and add following lines in it

venv
*.pyc
*.py~

Then,

git add .
git commit -m "hosting"
git push heroku master 

After launching it,

heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using

heroku config | grep HEROKU_POSTGRESQL

heroku run python manage.py syncdb

Then,'

heroku ps:scale web=1
heroku restart --app websitename

heroku open

Then you can see it is working......!!!!!!

heroku logs #for further info

If u have any doubts just clone my simple paint app using,

heroku git:clone -a latheefdjangopaint

and verify the codes and structure of Django app

No comments:

Post a Comment