Django: Access current user information from anywhere

When dealing with user authentication, it's often useful to include it into the view and template context.

There is a handy way of doing this and its already built into django.

In your settings, modify the MIDDLEWARE_CLASSES so it includes these two classes.


Once it's reloaded, you'll be able to access "request.user" in the view/context.

Additionally, if you need stuff like HTTP referrer and to check what browser they're using, it is available through:

  • request.META.get('REMOTE_ADDR', '')
  • request.META.get('HTTP_USER_AGENT', '')

Using the get() method is a bit safer as these values may not be set and it'll throw an exception.

Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog