Home > Backend Development > Python Tutorial > Django Heroku: complete deployment guide �

Django Heroku: complete deployment guide �

Mary-Kate Olsen
Release: 2025-01-29 10:12:12
Original
182 people have browsed it

Django   Heroku : Guide Complet de Déploiement �

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:

  • Python 3.x
  • git
  • A heroku account
  • a local functional django application
  • The HEROKU command line interface (Heroku CLI)

Project preparation:

  1. Project structure: Your project must look like this:
<code>my_project/
├── manage.py
├── my_project/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
└── Procfile</code>
Copy after login
  1. Configuration of dependencies:

Create the requirements.txt:

file
<code class="language-bash">pip freeze > requirements.txt</code>
Copy after login

Add the following outbuildings:

<code>django
gunicorn
psycopg2-binary
django-environ
whitenoise
dj-database-url</code>
Copy after login
  1. Django configuration for Heroku:

Modify the settings.py:

file
<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>
Copy after login
  1. Creation of the procile:

Create a Procfile file to the project root with the following content:

<code>web: gunicorn my_project.wsgi</code>
Copy after login

Deployment on Heroku:

  1. Creation of the Heroku application:
<code class="language-bash">heroku create mon-app-django</code>
Copy after login
  1. Configuration of environment variables:
<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>
Copy after login
  1. Postgresql database: (continued in the next section)

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template