-1

Say I have a login procedure in my website and that I login successfully.

After the user credential's successful authentication, I redirect to some page like

url(r'^account/profile/$',views.profile,name="profile")

Now my question is: if there are no arguments to the urlconf, how can I access the logged user's information in a view or template? (e.g. how can I know the username of that user in my template?)

Thanks in advance

user3264316
  • 332
  • 3
  • 18
  • possible duplicate of [How can I get the username of the logged-in user in Django?](http://stackoverflow.com/questions/16906515/how-can-i-get-the-username-of-the-logged-in-user-in-django) – sundar nataraj Apr 18 '14 at 11:27

2 Answers2

2

You can access the current User in a view with request.user.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

You can get the logged user using {{user.username}}

And if you want to send the user data to template,

def profile(request, ):
   user = User.objects.get(id=request.user.id)
   return render(request, 'profile.html', {'user_info': user})

profile.html:

{{ user_info }}

Note: user_info is current user instance. You can handle user data with this instance.

dhana
  • 6,487
  • 4
  • 40
  • 63
  • Is that feature implicit across django? That is, how would I call some of the currently logged user's methods in the view? – user3264316 Apr 18 '14 at 11:28
  • yes this future is implicit. you can find authenticated user also like `{{ user.is_authenticated }}` – dhana Apr 18 '14 at 11:31