SufferProgrammer SufferProgrammer - 1 year ago 156
Python Question

How to use flask flash messages in 2 different app.route

i'm fresh developer in flask framework, and i got stuck on this methods for showing error message, and i don't understand how to use flask flash message. i was think about 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 viwer. if it result 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 implement my idea.

@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'))

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

def login():
return render_template('login.html')

and in the html code i have this one

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

the question is how can i pass variable

error = 'Invalid username or password'

in url route

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

to url route


oh anyway you should know this one

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

note :
sorry if my english is bad

Answer Source

In Python, you have to call:

flask.flash('This is the message')

Then, in the template use get_flashed_messages to get the flashed messages and display them, e.g.:

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

Flask documentation has a very simple and nice example.

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