First, make sure that you have the SessionMiddleware and AuthenticationMiddleware middleware added to your MIDDLEWARE_CLASSES settings.
The current user is in the request object, you can get it:
def sample_view(request): current_user = request.user print current_user.id
request.user will provide you with a User object representing the current user who is logged in. If the user is not currently logged in, an AnonymousUser instance will be set for request.user . You can distinguish them using the is_authenticated field, for example:
if request.user.is_authenticated: # Do something for authenticated users. else: # Do something for anonymous users.
KZ Sep 27 '12 at 6:17 2012-09-27 06:17
source share