How to Dynamically Pull the UserID of the Logged-In User

Mateen.A.4093
Mateen.A.4093 Posts: 7 🌱
edited December 19 in Development

I have created a widget that pulls data for the logged-in user such as course progress but the widget will display unique data depending on what student is logged in.

I am trying to dynamically pull the user_id of the student logged in viewing the widget but I am getting stuck on what the current variable is for the User ID.

# Retrieve the user_id from query parameters, form data, or args
user_id = request.args.get('user_id') or request.form.get('user_id')

The above (in Python) is not currently working because I believe "user_id" is not the correct variable to call.

Purpose: Once we pull the user_id dynamically, we can call our APIs using that student's unique User ID.

Any help with this would be greatly appreciated.

Tagged:

Answers

  • @Mateen.A.4093,

    The simplest way to get that would be to include the {UserId} replacement string in the HTML of the widget. You could store it in a JS/Python/etc. variable, and then you should be able to reference it in your Python script. Normally the replacement strings may be used to personalize a message like an Announcement to show a user's name, but for fields like {UserId}, {OrgUnitId}, etc. are more useful for scenarios like this. You could technically pull it via other methods, but this is the simplest way to grab it dynamically.

    Hope that helps!

    -Johnny
    D2L LAM