1

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}}

aquatic7
  • 615
  • 1
  • 6
  • 19

1 Answers1

2

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)
miquelvir
  • 1,748
  • 1
  • 7
  • 21
  • Thank you. Now it shows as: Your role is: [] . How can I have it show only "developer"? – aquatic7 Mar 28 '21 at 16:09
  • I figured it out by in template put {{current_user.roles[0]}} Thank you. – aquatic7 Mar 28 '21 at 16:12
  • Ahh no, because a new problem has occurred. With {{current_user.roles[0]}} it ofc only show the first role if a person has multiple roles. How to show all roles names not being a tuple. – aquatic7 Mar 28 '21 at 16:18
  • print type(current_user.roles[0]) and let me know what type it has – miquelvir Mar 28 '21 at 16:34