This complete guide explains step by step how to deploy a Django application on Heroku and configure a postgreSql database.
Prerequisites:
Before you start, check that you have:
Project preparation:
<code>my_project/ ├── manage.py ├── my_project/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── requirements.txt └── Procfile</code>
Create the requirements.txt
:
<code class="language-bash">pip freeze > requirements.txt</code>
Add the following outbuildings:
<code>django gunicorn psycopg2-binary django-environ whitenoise dj-database-url</code>
Modify the settings.py
:
<code class="language-python">import os import dj_database_url from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = os.environ.get('SECRET_KEY', 'votre-clé-secrète-par-défaut') DEBUG = os.environ.get('DEBUG', 'True') == 'True' ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',') DATABASES = { 'default': dj_database_url.config( default=os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite3'), conn_max_age=600 ) } STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'staticfiles' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' MIDDLEWARE = [ # ... 'whitenoise.middleware.WhiteNoiseMiddleware', ]</code>
Create a Procfile
file to the project root with the following content:
<code>web: gunicorn my_project.wsgi</code>
Deployment on Heroku:
<code class="language-bash">heroku create mon-app-django</code>
<code class="language-bash">heroku config:set SECRET_KEY='votre-clé-secrète' heroku config:set DEBUG='False' heroku config:set ALLOWED_HOSTS='.herokuapp.com'</code>
The above is the detailed content of Django Heroku: complete deployment guide �. For more information, please follow other related articles on the PHP Chinese website!