pythonflask-securityflask-mongoengine

Show flask security roles in jinja2


I've created roles and assigned the to users. I can show the current logged in users name with {{current_user}} in jinja2. How do I show a users role(s) in jinja2?

For example: Hi {{current_user}}, you have the roles: {{roles}}


Solution

  • You need to pass those values as kargs in the render_template.

    from flask import render_template
    from flask_login import current_user
    
    @app.route("/roles")    
    def my_route():
        return render_template("roles.html", current_user=current_user, roles=current_user.roles)