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.
@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')
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 }}
</div>
{% 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
@app.route('/login')
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>
</div>
<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>
</div>
<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>
</div>
</div>
</form>
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!