2

I have a simple website where user can enter an email. A flash message is displayed if the email was entered.

If the form is submitted via click with the mouse on the submit button it works fine.

If the form is submitted via keyboard press "enter" the flash message is not displayed.

In both cases the form submits succesfully and adds an entry to the DB.

Its my second project and I never observed such behaviour, probably I never noticed it, will check everywhere now.

Thats how I display flash:

{% with messages = get_flashed_messages() %}
        {% if messages %}
            <div class="flashes col-xs-12">
            {% for message in messages %}
                <p>{{ message }}</p>
            {% endfor %}
            </div>
        {% endif %}
    {% endwith %}

The Code:

@app.route('/', methods=["GET","POST"])
def index():
    form = NewsletterForm()

    if form.validate_on_submit():
        newsletter_user = NewsletterUser(email=form.email.data.lower().strip())
        try:
            if newsletter_user:
                db_session.add(newsletter_user)                         
                db_session.commit()
                flash('Danke, wir melden uns bei Ihnen')
                return redirect(url_for("index"))
            else:
                flash('Email konnte nicht eingetragen werden')
                return redirect(url_for("index"))
        except IntegrityError:
            flash('Email wurde bereits eingetragen')
            return redirect(url_for("index"))

    return render_template('index.html', form = form)

Here is the part in the HTML (I moved the flash message under the button):

<div class="bg-image" alt="Baubedarf">

    <div class="index-container">

        <h1> Baubedarf finden. Bautechnik entdecken </h1>
        <p class="under-h1 hidden-xs hidden-sm"> Das Branchenverzeichnis für die Bauindustrie und Zulieferer </p>

        <div class="newsletter-form col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3">

            <form class="" method="POST" action="{{ url_for('index') }}">
                {{ form.csrf_token }}

                <h2> Werden Sie bereits von all Ihren potenziellen Kunden gefunden? </h2>

                <p class="hidden-xs"> Wenn nicht, dann bieten wir genau das richtige. Sichern Sie sich 6 Monate kostenlos alle Vorteile einer Premium Mitgliedschaft. Melden Sie sich direkt an! </p>

                {{ form.email(class="form-control", placeholder="Email") }}

                <button type="submit" class="btn"> Eintragen </button>

                <img alt="Banner" class="banner-index hidden-xs" src="../static/img/banner-index.png">

                {% if form.email.errors %}
                    {% for error in form.email.errors %} 
                        <p class="flashes-error-form"> {{ error }}</p>
                    {% endfor %}
                {% endif %}

                {% with messages = get_flashed_messages() %}
                    {% if messages %}
                    <div class="flashes col-xs-12">
                        {% for message in messages %}
                        <p>{{ message }}</p>
                        {% endfor %}
                    </div>
                {% endif %}
                {% endwith %}

            </form>
        </div>

    </div>

</div>

Here is the CSS style for the flash message:

.index-container .flashes {
    color: white;
    text-align: center;
    font-size: 20px;
}

EDIT

I created now a new site which should be displayed when the email was added successfully, but it seems the return redirect is not working thats probably why the flash message is not working (Eventhough it also works sometimes).

The code is as simple as it can be... and I still have no clue what causes this issue, because EVERY email is succesfully added to the DB.

How do I create a bounty on this question? Its the weirdest thing I even encountered.

New part, still same problem:

 try:
        db_session.add(newsletter_user)                         
        db_session.commit()
        flash('Danke, wir melden uns bei Ihnen')
        return redirect(url_for("newsletter_success"))
    except IntegrityError:
        flash('Email wurde bereits eingetragen')
        return redirect(url_for("index"))
    except InvalidRequestError as e:
        print e
        flash('Email wurde bereits eingetragen')
        return redirect(url_for("index"))
Roman
  • 3,563
  • 5
  • 48
  • 104
  • I tried to use the submit button and I don't get any flash message, the `flashes` class does not exists in the code – Or Duan Feb 08 '17 at 12:15
  • If you try via mouse click it will work – Roman Feb 08 '17 at 12:17
  • In my DB I already see some new test entries – Roman Feb 08 '17 at 12:18
  • Maybe you see them but I don't get any messages, I'm using the button with my mouse. might be cach issue? – Or Duan Feb 08 '17 at 12:18
  • I dont know, I deleted browser cache and use incognito for testing, I see the message if I use mouse click and dont see if I use enter button – Roman Feb 08 '17 at 13:05
  • Did you changed something? It is working for me now(both click and enter) – Or Duan Feb 08 '17 at 13:50
  • I am making minor changes, but its still not consistent for me, it works sometimes and sometimes it doesnt. I have to sleep a day and check it tomorrow. I tested right now and had to click 3 times with the mouse until the input was accepted, but yeah message was showing with enter and mouse – Roman Feb 08 '17 at 14:02
  • If you try more times you will notice its not consistent it works sometimes and sometimes it doesnt, I never had such an issue I am totally puzzled. – Roman Feb 08 '17 at 14:18
  • If I could reproduce the issue I would be so happy, I would know there is system... I dont even have a clue how this is possible. I just tested it, ot worked 2 times with enter and 3 times with mouse, then it didnt worked 2 times with enter, and then it worked again. Its not consistent. It happens only sometimes – Roman Feb 08 '17 at 14:30
  • Didn't see it on either click or enter. – ExperimentsWithCode Feb 08 '17 at 14:36
  • maybe there is a different place that render your message with `get_flashed_messages()`? like ajax requests/admin pages – Or Duan Feb 08 '17 at 15:50
  • I just started the project, this is everything I got, I have no other sites and also no js – Roman Feb 08 '17 at 19:01
  • Someone knows how to start a bounty? I have no clue, it seems it doesnt have a system. I just works sometimes and sometimes it doesnt – Roman Feb 09 '17 at 09:03

1 Answers1

0

Okay the problem was the SECRET_KEY for the session.

I forgot to change it. It was:

SECRET_KEY = os.urandom(24)

Solution:

SECRET_KEY = "something complex generated by maybe os.urandom"
Roman
  • 3,563
  • 5
  • 48
  • 104