Search code examples
pythonflaskcloud9-idecloud9

Flask flash not working after redirect


I'm having an issue in which the flash messages aren't showing the messages set before a redirect. I created a simple project to reproduce the problem. The redirect flash message doesn't show with or without the SERVER_NAME config file but the regular flash message always shows.

import sys
import os.path

from flask import Flask, flash, redirect, render_template, url_for


class TestConfig:
    DEBUG = True
    SERVER_NAME = 'project-username.c9users.io'

app = Flask(__name__)
app.secret_key = 'my_secret_key'


@app.route('/')
def index():
    flash('This is a regular test flash')
    return render_template('test.html')


@app.route('/flash')
def flash_it():
    flash('This is a redirect test flash')
    return redirect(url_for('index'))


app.run(host='0.0.0.0', port=8080)

And the template:

{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}

Update

I attempted to call this from the command line using localhost instead of the domain name that I have through the cloud9 interface. And it worked fine.

$ wget -q -O - "$@" http://localhost:8080/flash

This is a redirect test flash

This is a regular test flash

I have the SERVER_NAME variable set to the correct name but it still doesn't work.


Solution

  • The problem turns out to be related to the SERVER_NAME. It should not contain the subdomain name.

    Changing the config to the following makes it work with the Cloud9 domain name:

    class TestConfig:
        DEBUG = True
        SERVER_NAME = 'project-username.c9users.io'