Search code examples
djangodjango-templatesdjango-viewsdjango-authenticationdjango-users

If a django view does User.get on a username while another user is logged in, is the user context lost?


I have a Teacher/Student django app. When a Teacher is logged in, and wants to see the Student's progress, this is what I am doing:

  1. Teacher is logged in and is the request.user. She requests reports for a Student with username say 'mystudent'
  2. studentuser = get_object_or_404(User, username='mystudent')
  3. And then it proceeds to retrieve the data of that Student to display it to the Teacher

In my template, I have:

Teacher: {{ user.username }}
Student Info:

I get the correct Student Info, but I do not see the Teacher's username. My template also has navigation based on whether the user is logged in or not and for this page, I get the not_logged_in view of my navigation bar.

However, now if I browse over to other pages, which also display Teacher: {{ user.username }}, the username is seen, and the logged_in navigation bar is displayed.

So it is just for this one template that confused with the User though the actual user login (Teacher) is valid and retained.

Could someone please explain what is going wrong here?


Solution

  • Pasting my comment since it turns out it was the answer.

    No. There is no magic like that. While we can't tell since you haven't posted your code, my guess is that this one view doesn't use say RequestContext and the user variable is simply not in the context.