This is a useless webapp (just for practice). You can visit here to see my hosted page.
- At first create account or login in heroku
- Download
heroku-cli
from here or visit the website
pip install virtualenv
virtualenv anyname
- After creating virtual environment activate your vitualenv
pip install django gunicorn django-heroku
You can install specific version like pip install django==2.2
- Copy / Transfer your django-project in that virtual environment
- Add your dependencies to requirements.txt by typing in the terminal
pip freeze > requirements.txt
- Don't forget to add this in settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
- This file is used to explicitly declare your application’s process types and entry points. It is located in the root of your repository
web: gunicorn yourprojectname.wsgi:application --log-file -
- Add the following
import
statement to the top ofsettings.py
import django_heroku
- Then add the following to the bottom of
settings.py
# Activate Django-Heroku.
django_heroku.settings(locals())
- You can also made these changes in
settings.py
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ["*", "localhost"]
# Application definition
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # --> only add this line
'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',
]
- Open your terminal / cmd and run the following commands
heroku login
- After run
heroku login
your default browser will be open and after logged in heroku you can see:
C:\Users\SUPRATIM\Desktop\UselessDjangoApp>heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/e1625921-dddc-400f-adc3-8f570d48ad0c?requestor=SFMyNTY.g2gDbQAAAAw0OS4zNy4zOS4yNTBuBgCYFdJzfgFiAAFRgA.enp9fW26_s1Hzn_VloGHmZpz3hi9QEY07WSUne6sOc4
Logging in... done
Logged in as supratimm531@gmail.com
- Now create your
heroku-app
using this command
heroku create yourappname
- After run this command you can see:
C:\Users\SUPRATIM\Desktop\UselessDjangoApp>heroku create yourappname
Creating app... done, yourappname
https://yourappname.herokuapp.com/ | https://git.heroku.com/yourappname.git
- Now follow these steps
git status
git init
git remote add heroku https://git.heroku.com/yourappname.git
git remote -v
git status
git add .
git status
git commit -m "initial commit by yourname"
- Add
runtime.txt
in the root folder of your project / repository and write the current python version (used for your project) in the file
python-3.9.4
- If you are using
django==2.2
then change the version ofpsycopg2==2.9.x
withpsycopg2==2.8.6
like this
pip uninstall psycopg2==2.9.x && pip install psycopg2==2.8.6
pip freeze > requirements.txt
- After this check your
requirements.txt
at-least once
dj-database-url==0.5.0
Django==2.2
django-heroku==0.3.1
gunicorn==20.1.0
psycopg2==2.8.6
pytz==2021.3
sqlparse==0.4.2
whitenoise==5.3.0
Just run this command in your terminal / cmd
git push heroku master
Run these 2 following commands
heroku run python manage.py migrate
heroku run python manage.py createsuperuser