Yebach
Yebach

Reputation: 1691

web2py sqlform error

I am new at web2py and i am struggling with it

I am trying to crate a registration and login stuff. In models I have the following code where I create tabel for new users

usersdb = DAL('postgres://postgres:postgres@localhost/'+ request.vars['school'], migrate=True)
auth = Auth(usersdb, hmac_key=Auth.get_or_create_key())
usersdb.define_table(
    'users',
    Field('first_name', length=128, default=''),
    Field('last_name', length=128, default=''),
    Field('email', length=128, default='', unique=True),
    Field('address', length=256, default=''),
    Field('postcode', length=128, default=''),
    Field('city', length=128, default=''),
    Field('password', 'password', length=512, readable=False, label='Password'),
    Field('registration_key', length=512, writable=False, readable=False, default=''),
    Field('reset_password_key', length=512, writable=False, readable=False, default=''),
    Field('registration_id', length=512, writable=False, readable=False, default=''),
    format='%(first_name)s %(last_name)s')

So now in controler I have the following code:

usersdb = DAL('postgres://postgres:postgres@localhost/'+ request.vars['school'], migrate=True)
auth = Auth(usersdb, hmac_key=Auth.get_or_create_key())
auth.settings.registration_requires_approval = True
form = SQLFORM(usersdb.users)
if form.process(session=None, formname='test').accepted:
    response.flash = 'form accepted'
elif form.errors:
    response.flash = 'form has errors'
else:
    response.flash = 'please fill the form'
 # Note: no form instance is passed to the view

 #set sample validator (do not allow empty nor duplicate names)
return dict(form=form,config=config)  

The error is loged at SQLFORM (usersdb.users). 'users'

I don't know how actually this stuff works (connecting database to controler). I am hacking a code written by smb. else.

Thank you for any advice or short explanation.

Bye

Upvotes: 0

Views: 786

Answers (1)

Anthony
Anthony

Reputation: 25536

You should not repeat the definition of userdb or auth in the controller. Model files are executed on every request (except for conditional model files), and the controller is executed in an environment that contains any objects defined in the models. In your controller, you are overwriting the original userdb object with a fresh one, and you are defining auth again but not re-creating the "users" table -- hence there is no longer a "users" table on userdb.

Just remove the userdb and auth definitions from the controller.

Upvotes: 1

Related Questions