Mezzanine on Heroku

by jackvz


Mezzanine on Heroku

A clean install of Mezzanine CMS with the Cartridge e-commerce extension, with an API for connecting iOS and Android apps for example, that integrates with Stripe for payment processing, and that is configured to deploy to Heroku and to use Amazon S3 for media storage.

Do an automated deploy to Heroku and select one of the free themes for Mezzanine, Flat, Moderna, Nova or Solid: Deploy

  1. Build
  2. Deploy
  3. Theme


Install Python and PostgreSQL

Start a Python virtual environment

Clone the repository and run:

virtualenv env
source env/bin/activate
pip3 install -r requirements.txt

Configure the system

In onlineshop/ and onlineshop/, set values for the following:

Install a clean database

Create a local PostgreSQL database called onlineshop

Run all migrations:

python3 makemigrations
python3 migrate

If the theme is not used and all default settings are restored, no migrations are necessary so just create the database:

python3 createdb --noinput --nodata

Run the development web server

Collect the static files:

python3 collectstatic

Run a Django development server:

python3 runserver

Browse to http://localhost:8000/

Run a Gunicorn web server similar to Heroku:

heroku local -f local-heroku.procfile

Browse to http://localhost:5000/

For managing content with Mezzanine, browse to http://localhost:8000/admin

The default super user is 'admin' and the password is 'default'. You should change this.

To clear the static file cache:

python3 clear_cache

Work with the API

Add an API key via the admin and note the token and secret key: http://localhost:8000/admin/rest_framework_api_key/apikey/add/

Browse to the API documentation at http://localhost:8000/api/docs, click the Authorize button and enter the token and secret key, and then try out any of the API calls.

Deactivate the virtual environment

If you need to recreate the virtual environment, perhaps to clear any changes you made directly to any of the installed Python packages, deactivate the virtual environment first:



Download the Heroku CLI


heroku login

Create an app:

heroku create

Note the Heroku app name, and add the Heroku Git repository as a remote to this Git repository:

heroku git:remote -a [heroku-app-name]

Configure New Relic:

heroku addons:create newrelic:wayne
heroku config:set NEW_RELIC_APP_NAME=[heroku-app-name]

Deploy the code:

git push heroku master

If the Amazon S3 media file storage configuration is removed, then the Mezzanine Media Library in staticfiles/media is included in the Git repo, and can be included in the deploy.

Create a PostgreSQL database for Mezzanine on Heroku

heroku run python createdb --noinput --nodata --settings=onlineshop.heroku_settings


View production site:

heroku open

Access the production PostgreSQL database:

heroku pg:psql [heroku-database-name] --app [heroku-app-name]

Put Heroku app in maintenance mode to display a user friendly offline page:

heroku maintenance:on

Run a Mezzanine/Django Python shell on Heroku, for example, create a super user:

heroku run python shell --settings=onlineshop.heroku_settings`
from django.contrib.auth.models import User
user=User.objects.create_user('foo', password='bar')

Content publication workflow

Create a local Mezzanine PostgreSQL database backup:

pgpassword=[db-password] pg_dump -Fc --no-acl --no-owner -h localhost -U [postgres-user] [db-name] > mydb.dump

Generate a signed URL for a database backup that was uploaded to Amazon S3 storage:

aws s3 presign s3://[s3-bucket-name]/mydb.dump

Restore a Mezzanine PostgreSQL database backup to Heroku from Amazon S3, using a signed URL:

heroku pg:backups:restore [aws-s3-signed-url] [heroku-db-name] --confirm [heroku-appname]

If only database structural changes were made and no actual data changed, then Heroku could be updated with Django migrations:

heroku run "python makemigrations [django-appname] --settings=onlineshop.heroku_settings && python migrate [django-appname] --settings=onlineshop.heroku_settings"


For an excellent article on creating a theme, see this Bit of Pixels article.