How to use flask flash messages in 2 different app.route
<p>I'm fresh developer in flask framework, and I got stuck on these methods for showing error message, and I don't understand how to use flask flash message. I was thinking about it for 3 or 4 days and I got some idea how to handle this problem. so my plan is simple, I make some authentication login in my viewer. If it results output with value false, it will produce error code and that's error code will be showed in my login page. here is how I've implemented my idea.</p>

<pre><code>@app.route('/login/process', methods = ['POST'])
def loginprocess():
    username = request.form.get('user_name')
    passwd = request.form.get('user_passwd')
    userAdminAuth = userLogin.checkUserAdmin(username, passwd)
    userMemberAuth = userLogin.checkUserMember(username, passwd)
    if userAdminAuth == True and userMemberAuth == False:
      session['logged_in'] = True
      session['username'] = username
      return redirect(url_for('admin'))

    elif userAdminAuth == False and userMemberAuth == True:
      session['logged_in'] = True
      session['username'] = username
      return redirect(url_for('member'))

    else:
      error = 'Invalid username or password'
      return redirect(url_for('login'))

@app.route('/login')
def login():
    return render_template('login.html')
</code></pre>

<p>And in the html code I have this one</p>

<pre><code>{% if error %}
    &lt;div class="alert alert-danger" role="alert"&gt;
        &lt;span class="glyphicon glyphicon-exclamation-sign"&gt;&lt;/span&gt;
        &lt;span class="sr-only"&gt;Error&lt;/span&gt;
        {{ error }}
    &lt;/div&gt;
{% endif %}
</code></pre>

<p>The question is how can I pass variable </p>

<pre><code>error = 'Invalid username or password'
</code></pre>

<p>In url route</p>

<pre><code>@app.route('/login/process', methods=['POST'])
</code></pre>

<p>To url route</p>

<pre><code>@app.route('/login')
</code></pre>

<p>Oh anyway you should know this one</p>

<pre><code>&lt;form action="/login/process" method="post"&gt;
  &lt;div class="form-group"&gt;
    &lt;div class="input-group"&gt;
      &lt;div class="input-group-addon icon-custumized"&gt;&lt;span class="glyphicon glyphicon-user"&gt;&lt;/span&gt;&lt;/div&gt;
        &lt;input type="text" name="user_name" class="form-control form-costumized" placeholder="Username"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;div class="input-group"&gt;
        &lt;div class="input-group-addon icon-custumized"&gt;&lt;span class="glyphicon glyphicon-lock"&gt;&lt;/span&gt;&lt;/div&gt;
          &lt;input type="password" name="user_passwd" class="form-control form-costumized" placeholder="Password"&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="btn-toolbar" role="toolbar" aria-label="action"&gt;
        &lt;div class="btn-group" role="group" aria-label="action"&gt;
          &lt;a class="btn btn-default btn-customized" href="#"&gt;&lt;span class="glyphicon glyphicon-list-alt"&gt;&lt;/span&gt; &lt;span class="textsize"&gt;Register&lt;/span&gt;&lt;/a&gt;
          &lt;button type="submit" class="btn btn-default btn-customized"&gt;&lt;span class="textsize"&gt;Login&lt;/span&gt; &lt;span class="glyphicon glyphicon-menu-right"&gt;&lt;/span&gt;&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p>In Python, you have to call:</p>

<pre><code>flask.flash('This is the message')
</code></pre>

<p>Then, in the template use <code>get_flashed_messages</code> to get the flashed messages and display them, e.g.:</p>

<pre><code>{% with messages = get_flashed_messages(with_categories=True) %}
    {% for category, message in messages %}
        &lt;div class="flash {{category}}"&gt;{{message}}&lt;/div&gt;
    {% endfor %}
{% endwith%}
</code></pre>

<p><a href="http://flask.pocoo.org/docs/0.11/patterns/flashing/" rel="nofollow">Flask documentation</a> has a very simple and nice example.</p>

<p>The fact that the message is flashed in one route and displayed in another is not a problem. That is exactly the use case <code>flask.flash</code> was designed for!</p>

python python-3.x flask flash-message