views:

64

answers:

3

In my function I read user's data from session and store them in a dictionary. Next I'm sending it to 'register' function from registration.backend but the function somehow get's it empty and a KeyError is thrown. Where are my data gone ? The code from function calling 'register' function :

data = request.session['temp_data']
email = data['email']

logging.debug(email)

password1 = data['password1']
userdata = {'email': email, 'password1': password1}

logging.debug(userdata)

backend = request.session['backend']

logging.debug(backend)

user = backend.register(userdata)

And the register function (whole source here : http://bitbucket.org/ubernostrum/django-registration/src/tip/registration/backends/default/init.py ) :

class DefaultBackend(object):
    def register(self, request, **kwargs):
        logging.debug("backend.register")
        logging.debug(kwargs)

        username, email, password = kwargs['email'], kwargs['email'], kwargs['password1']

Debug after invoking them :

2010-07-09 19:24:35,020 DEBUG [email protected]
2010-07-09 19:24:35,020 DEBUG {'password1': u'a', 'email': u'[email protected]'}
2010-07-09 19:24:35,020 DEBUG <registration.backends.default.DefaultBackend object at 0x15c6090>
2010-07-09 19:24:35,021 DEBUG backend.register
2010-07-09 19:24:35,021 DEBUG {}

Why the data could be missing ? Am I doing something wrong ?

@edit for Silent-Ghost

 register() takes exactly 2 arguments (3 given)

 112.  backend = request.session['backend']
 113. logging.debug(backend)
 114. user = backend.register(request, userdata) 
+3  A: 

Judging by the method's signature:

  1. you need to unpack your dictionary
  2. you need to pass relevant request variable

Something like this:

backend.register(request, **userdata)

Assuming register is a method on backend instance.

SilentGhost
but then I'm getting error "function register() accepts 2 arguments, 3 given" :/
muntu
@muntu: then it's some other function `register`, not the one you're showing us
SilentGhost
+3  A: 

No need to mess with ** in register method. What you want to do is simply pass dictionary to register method:

user = backend.register( request, userdata ) # you need to pass request as definition says

def register( self, request, userdata ): # note lack of **
    logging.debug("backend.register")
    logging.debug( userdata ) # should work as expected

    username, email, password = userdata['email'], userdata['email'], userdata['password1']
cji
It's only a guess, but is `backend` an object or a class? Try writing `backend().register( request, userdata )` maybe?
cji
great, now it works !
muntu
A: 

this perfectly work

class Logging():
    def debug(self,f):
        print f
class DefaultBackend(object):
    def register(self, request, **kwargs):
        logging.debug("backend.register")
        logging.debug(kwargs)

        username, email, password = kwargs['email'], kwargs['email'], kwargs['password1']

class Request:
    def __init__(self):
        self.session = {}
request = Request()
logging=Logging()
request.session['temp_data']={'password1': u'a', 'email': u'[email protected]'}
request.session['backend']=DefaultBackend()



data = request.session['temp_data']
email = data['email']

logging.debug(email)

password1 = data['password1']
userdata = {'email': email, 'password1': password1}

logging.debug(userdata)

backend = request.session['backend']

logging.debug(backend)

user = backend.register(request,**userdata)
Xavier Combelle