I have deployed a Flask web app to a Digital Ocean droplet running Ubuntu 18.04. The web app is simply a landing page that has a single form that adds a visitor's email address to a Mailchimp mailing list.

The web app works perfectly in development, however the form submission does not work in production. Submitting the form just reloads the page. It does not submit the email address to the mailing list nor is the 'thank you' page displayed.

In development, I am using the default Flask WSGI server to directly access the web app (i.e. localhost:5000).

In production, I am using Nginx in conjunction with Gunicorn. As a sanity test, I ran the app using Flask's default WSGI server in production and it worked fine.

Aside from using Nginx and Gunicorn, the web app code and configuration (e.g. Mailchimp list id / api key) are identical across both environments.

What could be causing this problem?

  • 1
    What's in your app logs and web server logs? – Michael Hampton Jan 11 at 16:43
  • Adding more detailed logs revealed the problem, which I described in the answer below. – Josh Jan 12 at 5:42

The problem was due to the Mailchimp API key not being read in from my .flaskenv file.

In development, it appears that the .flaskenv file is implicitly loaded by python-dotenv but in production you need to explicitly load the file.

import os
from dotenv import load_dotenv

# explicitly load .flaskenv
basedir = os.path.abspath(os.path.dirname('__file__'))
load_dotenv(os.path.join(basedir, '.flaskenv'))

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY')

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.