1
votes

I'm building a Web App with Python / Flask. I'm protecting a view with the @login_required decorator. If user is not logged in it will redirect to my 'login' view URL, and add a flash message with ("You need to be logged in to view this page."). This flash message is then displayed after the redirected view ('login') is loaded. How can I add a category to this message since it looks its coming from @login_required?

Here is my view function:

@app.route('/')
@app.route('/index')
# This is the decorator that redirects my request to the
# 'login' view, and adds a flash message. 
@login_required
#
def index():
    ...
    return render_template(...)

In order to pass a category to a flash message I add "manually" I can do as follows.

flash_message = flash("User needs to be logged in to view this page", "category")
1

1 Answers

4
votes

I ve just came across this question because I had the same problem. You probably figured it out, but what you need to add to customize @login_required is

login_manager.login_view = "users.login"

login_manager.login_message = "User needs to be logged in to view this page"

login_manager.login_message_category = "warning"