Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions django-hosting-heroku/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Hosting a Django Project on Heroku

This is a companion project to a Real Python tutorial about [hosting a Django project on Heroku](https://realpython.com/django-hosting-on-heroku/). It's a bare-bones Django project without any views.
This is a companion project to a Real Python tutorial about [hosting a Django project on Heroku](https://realpython.com/django-hosting-on-heroku/). It's a bare-bones Django project without any views, configured for Heroku with Gunicorn, WhiteNoise, dj-database-url, and psycopg.

You'll need Python 3.12 or later, Git, and the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli). The project pins Python 3.15 for Heroku in its `.python-version` file.

## Instructions

Expand All @@ -26,7 +28,8 @@ Deploy the code to Heroku:
```shell
$ heroku login
$ heroku create
$ heroku config:set DEBUG=True SECRET_KEY=$(head -c 32 /dev/urandom | base64)
$ heroku addons:create heroku-postgresql:essential-0
$ heroku config:set SECRET_KEY=$(head -c 32 /dev/urandom | base64)
$ git push heroku master
$ heroku open
```
Expand All @@ -42,11 +45,4 @@ $ source venv/bin/activate
(portfolio) $ heroku local
```

Navigate your web browser to <http://0.0.0.0:5000/>

**Note:** If you're on a Mac, then you may run into issues with the `heroku local` command above. To mitigate it, try commenting out the line with your `DATABASE_URL` variable from the `.env` file. Alternatively, you can uninstall the `psycopg2` driver and install its binary counterpart:

```console
$ pip uninstall psycopg2
$ pip install psycopg2-binary
```
Navigate your web browser to <http://0.0.0.0:5006/>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.toptal.com/developers/gitignore/api/python,pycharm+all,django
# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm+all,django

Expand Down Expand Up @@ -107,7 +106,22 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -150,6 +164,13 @@ dmypy.json
# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### PyCharm+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
Expand Down Expand Up @@ -214,6 +235,9 @@ atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
Expand All @@ -227,20 +251,13 @@ fabric.properties
.idea/caches/build_file_checksums.ser

### PyCharm+all Patch ###
# Ignores the whole .idea folder and all .iml files
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
# Ignore everything but code style settings and run configurations
# that are supposed to be shared within teams.

.idea/
.idea/*

# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023

*.iml
modules.xml
.idea/misc.xml
*.ipr

# Sonarlint plugin
.idea/sonarlint
!.idea/codeStyles
!.idea/runConfigurations

### Python ###
# Byte-compiled / optimized / DLL files
Expand Down Expand Up @@ -284,7 +301,19 @@ modules.xml
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm

# Celery stuff

Expand All @@ -306,4 +335,21 @@ modules.xml

# Cython debug symbols

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.

### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml

# ruff
.ruff_cache/

# LSP config files
pyrightconfig.json

# End of https://www.toptal.com/developers/gitignore/api/python,pycharm+all,django
staticfiles/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.15
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: python manage.py runserver 0.0.0.0:$PORT
web: gunicorn portfolio.wsgi
release: python manage.py migrate
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
https://docs.djangoproject.com/en/6.1/howto/deployment/asgi/
"""

import os
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
"""
Django settings for portfolio project.
Generated by 'django-admin startproject' using Django 3.2.5.
Generated by 'django-admin startproject' using Django 6.1.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
https://docs.djangoproject.com/en/6.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
https://docs.djangoproject.com/en/6.1/ref/settings/
"""

import os
from pathlib import Path

import django_heroku
import dj_database_url

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = 'django-insecure-=bad%3%tbrw8(ohgu2uyi7)acv!$1br3=j-o)i@b+1d9g-sd1('
SECRET_KEY = os.environ["SECRET_KEY"]

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "0.0.0.0", ".herokuapp.com"]
CSRF_TRUSTED_ORIGINS = ["https://*.herokuapp.com"]


# Application definition
Expand All @@ -43,6 +45,7 @@

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand All @@ -60,7 +63,6 @@
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
Expand All @@ -73,18 +75,18 @@


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# https://docs.djangoproject.com/en/6.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
"default": dj_database_url.config(
default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}",
conn_max_age=600,
)
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/6.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -103,27 +105,35 @@


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
# https://docs.djangoproject.com/en/6.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
# https://docs.djangoproject.com/en/6.1/howto/static-files/

STATIC_URL = "/static/"
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

STORAGES = {
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# Email
# https://docs.djangoproject.com/en/6.1/topics/email/#topic-email-configuration

django_heroku.settings(locals())
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.console.EmailBackend",
},
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""portfolio URL Configuration
"""
URL configuration for portfolio project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
https://docs.djangoproject.com/en/6.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
https://docs.djangoproject.com/en/6.1/howto/deployment/wsgi/
"""

import os
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
asgiref==3.4.1
dj-database-url==0.5.0
Django==3.2.5
django-heroku==0.3.1
psycopg2==2.9.1
pytz==2021.1
sqlparse==0.4.1
whitenoise==5.2.0
asgiref==3.13.0
dj-database-url==3.1.2
Django==6.1.4
gunicorn==27.0.1
psycopg==3.3.9
psycopg-binary==3.3.9
sqlparse==0.6.1
whitenoise==6.12.0

This file was deleted.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
https://docs.djangoproject.com/en/6.1/howto/deployment/asgi/
"""

import os
Expand Down
Loading
Loading