get user info in LMS

947 views
Skip to first unread message

campi

unread,
Jan 27, 2016, 11:11:28 AM1/27/16
to General Open edX discussion
Hi,

I have noted that in the HTML templates of EDX LMS (e.g. Navigation.html, courseware/courses.html, etc.) I can access a Python variable called user.

This is an example code portion of navigation.html

      <li class="primary">
        <a href="${reverse('dashboard')}" class="user-link">
          <i class="icon fa fa-home" aria-hidden="true"></i>
          <span class="sr">${_("Dashboard for:")}</span>
          <div>
          ${user.username}
          </div>
        </a>
      </li>
      <li class="primary">

This ${user.username} sentence prints the username of the user currently logged. It is also possible to print the current user's email by using ${user.email}

However, when I try to print the user's country (I have set the country registration field as required) in this way ${user.country} an error arises as the attribute 'country' does not exist in the variable user.

AttributeError: 'User' object has no attribute 'country'

Where is this 'User' object defined in the code? Can I modify it adding a country attribute in order to obtain the user's country in the LMS code?

Thanks in advance for your help.

David Adams

unread,
Jan 27, 2016, 2:20:52 PM1/27/16
to General Open edX discussion
Campi,

The country attribute is on the UserProfile object not the User object. The model file for UserProfile is common/djangoapps/student/models.py.

Fortunately, in Django template language you can traverse relations; there is a one to one between user and profile.

If you replace ${user.username} with ${user.profile.country} you should have it.

"profile" is the name of the relation between User and UserProfile.

In terms of tables in the db, UserProfile has the name "auth_userprofile".

Regards
David

campi

unread,
Jan 28, 2016, 4:35:13 AM1/28/16
to General Open edX discussion
Thank you very much for your help, it was very useful !

Robi Renz

unread,
Feb 1, 2016, 6:54:33 AM2/1/16
to General Open edX discussion
Hi Campi

I'm interested in doing this exact same thing..

I'm trying to send data off to and external web address as parameters.

but when I add the variable names and look at the course html I only visually see the ${user.username} on screen..
the only one I can get to translate automatically is the %%USER_ID%%, and that one only puts student instead of the actual ID.
any hints on how to tackle this would be greatly appreciated..

Best Regards...

Roberto Renz

Robert Raposa

unread,
Feb 5, 2016, 10:41:40 AM2/5/16
to edx-...@googlegroups.com
Hi Robi.

What template are you in?  Are you in a context with other variables that are being properly replaced with this same syntax?  Or are you using Django Templates rather than Mako Templates?

Thanks.

Robert

--
You received this message because you are subscribed to the Google Groups "General Open edX discussion" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/edx-code/bdb36fc5-3b53-4438-8da2-7b0e01955090%40googlegroups.com.



--

Robert Raposa

edX | Software Architect  | rra...@edx.org  

141 Portland Street, 9th floor

Cambridge, MA 02139

http://www.edx.org

http://www.e-learn.nl/media/blogs/e-learn/edX_Logo_Col_RGB_FINAL.jpg?mtime=1336074566

Roberto Renz

unread,
Feb 5, 2016, 2:56:53 PM2/5/16
to edx-...@googlegroups.com
Hi Robert,

I'm trying to use the %%USER_ID%% just on a unit html content. it works but It changed to  student instead of the actual  user ID.
so I tried playing with the templates for lms and cms and I can add stuff to the footer or whatever other html file, but the variables are like ${user.id}   or ${user.username}   and they work fine, I do get the value of the variables..
what I would like to do is use those variables but on a normal html unit so I can use them in an iframe however it doesn't convert the variables, I just get the same string.

Now I'm guessing, this is probably where I have to create an xblock and use that in advanced blocks. but that is where I'm stuck, unless somebody has any other ideas.

Best Regards...

Roberto Renz
You received this message because you are subscribed to a topic in the Google Groups "General Open edX discussion" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/edx-code/CAKZ_z5mvGkem38ORCwUbNZOTPEiA4ggGP9ARj8n3Uaz0gCz9Hg%40mail.gmail.com.

Robert Raposa

unread,
Feb 10, 2016, 1:15:58 PM2/10/16
to edx-...@googlegroups.com
Sorry for the delay Robi.

It looks like you are looking for something like the Keyword functionality supported in bulk emails, but that doesn't exist for the HTML Component. 

I did find the following code which seems to explain how %%USER_ID%% is getting replaced.  I was trying to better understand the comment in that code, to see if that helps indicate a direction for you, but I haven't yet gotten an answer and didn't want to leave you with no response.

Robert

Nabil Raddaoui

unread,
Apr 28, 2016, 4:16:56 PM4/28/16
to General Open edX discussion
Hi,

Can you please put your code in github it will helpfull for me.

Thank you

Nabil

Leslie Huin

unread,
Jan 14, 2017, 8:16:10 AM1/14/17
to General Open edX discussion
Hi

It's normal. It's because you are testing it in the studio. It will work then in the actual MOOC. At last, it works for me ;)

Nate Aune

unread,
Jan 17, 2017, 12:33:24 AM1/17/17
to General Open edX discussion
We needed to be able to grab the user's email address so that we could send it to an external API, so we changed the file:

in the class HtmlModuleMixin(HtmlBlock, XModule) (lines added are in green)

    def get_html(self):
        data = self.data
        if self.system.user_is_staff:  
            from django.contrib.auth.models import User
            user = User.objects.get(id=self.system.user_id)
            data = data.replace("%%USER_EMAIL%%", user.email)
        elif self.system.anonymous_student_id:
            data = data.replace("%%USER_ID%%", self.system.anonymous_student_id)
            if getattr(self.system, 'get_real_user', None):
                user = self.system.get_real_user(self.system.anonymous_student_id)
                if user and user.is_authenticated():
                    data = data.replace("%%USER_EMAIL%%", user.email)
        
        return data
Reply all
Reply to author
Forward
0 new messages