25
loading...
This website collects cookies to deliver better user experience
skip
this step. For this tutorial I am going to use pipenv
for virtual enviorment. You could use anything for that. So the first step is to create the virtual enviorment and install the dependencies.pipenv install django
pipenv shell
django-admin startproject main .
main
and using a dot to avoud creating another folder called main
to create the project. After you done all of that, now we have finished out first part.settings.py
file and do this changes.import os # at the top
SECRET_KEY = os.environ.get("SECRET_KEY", "teyd23e2efvdgsf3dv2d362")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', False)
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
pipenv install dj-database-url gunicorn psycopg2 whitenoise
settings.py
file and add some more settings.import dj_database_url
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware', # add this new 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',
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
whitenoise
to managing out static files and dj_database_url
to connecting with production database. After you done all that, now we need to create a new file called Procfile
and paste this lines of code.web: gunicorn main.wsgi
gunicorn main.wsgi
. Remember put your project name instead of mine eg. web: gunicorn <your project name>.wsgi
.requirements.txt
file, Heroku will install all the dependencies from Pipfile
. But if you're using virtual env then Don't forget to create the requirements.txt
file.git
you alredy know all of these command. to commit all of the changes you have done write these command.git init
git add .
git commit -m "Initial commit"
django application to Heroku
.heroku create <write your project name>
Heroku
to see your website live. To do that write this command in your terminal.git push heroku master
Sometime heroku gives error if the name alredy exists to better if you don't pass any name, heroku will create a reandom name for your project.