views:

59

answers:

2

I have a decorator that I use for my views @valid_session

from django.http import Http404

def valid_session(the_func):
"""
function to check if the user has a valid session
"""
def _decorated(*args, **kwargs):        
    if ## check if username is in the request.session:
        raise Http404('not logged in.')
    else:
        return the_func(*args, **kwargs)
return _decorated

I would like to access my session in my decoartor. When user is logged in, I put the username in my session.

A: 

You could pass the request (or just the session) in as a parameter to the decorator. I just don't know how to get at it to pass it in. I was trying to figure out something similar last night.

Tom
+3  A: 

Will something like the following solve your problem:

def valid_session(func):
    def decorated(request, *args, **kwargs):
        print request.session
        return func(request, *args, **kwargs)
    return decorated
Satoru.Logic
nope. It doesn't have the session variables.
@ed1t, have you put username to request.session before reading it?
Yaroslav
@ed1t Is `django.contrib.sessions` installed? What about the MIDDLEWARE_CLASSES list in your settings.py? Does it have something like a session middleware in it?
Satoru.Logic