Django App on Heroku- Easy-Way
I often find People find hard when it comes to manual deployment but we must always deploy manually as its good practice to do that, It's just like using git bash/shell for commands instead of using Github Desktop as its talking to computer in his language and most importantly learning out about logs and errors in that. Read Further....
Prerequisites:-
A system loaded with any OS for a test Django App for deployment, A GitHub Account, A Heroku account.
Step by step guide for deployment of django APP on heroku
step1 :
Create Procfile often get error just replace P with lowercase p.
Note: as in my case happend it.
Replace with your project name
web: gunicorn You_ProjectName.wsgi --log-file -
step2 :
Install gunicorn
pip install gunicorn
step3 :
Prepare a requirements.txt okkkk...Now You want to tell server what you have & it must dowload for you. This file in your project is used to tell the server what dependencies.
pip freeze > requirements.txt
and Update settings.py file Set the following changes.
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1','sampledomain.com']
step4 : Update
Original Middle ware Code
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Updated Middle ware Code
Install whitenoise dependency
pip install whitenoise
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
step3 : Update
Original urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("Home.urls")),
]
Updated urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("Home.urls")),
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Also don't Forget to
from django.views.static import serve
from django.conf.urls import url
You have done most of the part just open heroku account do following: Importantanly commit all changes to github.
By clicking on “New” you can open the Create New App. -> Now switch to deploy panel -> Connect to github and select Repo -> Click “Deploy” under “Manual Deploy”
TADA Your website is ready... shareit.