Sorry, you're right. db(dk2,'jusers).insert was one of many desperate
attempts at a solution. I forgot I hadn't changed it back.
So, i'll post the controller and model again in the original form...
Model:
from datetime import datetime, date, time
today = date.today()
now = datetime.utcnow()
# ALWAYS tell python where the DB is...PLEASE.
dk2=SQLDB("mysql://root:[email protected]:3306/dk2")
dk2.define_table('jusers',
SQLField('displayname',length=32,requires=
[IS_NOT_EMPTY(),IS_LENGTH(32),]),
SQLField('username',length=40,requires=
[IS_NOT_EMPTY(),IS_EMAIL(), IS_NOT_IN_DB(dk2,'jusers.username')]),
SQLField('password',requires=IS_NOT_EMPTY()),
SQLField('regdate','datetime',default=now),
SQLField('retiredate','datetime',default=now),
SQLField('active','boolean',default=True))
dk2.define_table('jtype',
SQLField('name',length=40,requires=IS_NOT_EMPTY
()),
SQLField('user',dk2.jusers))
dk2.define_table('journals',
SQLField('user',dk2.jusers),
SQLField('name',length=40,requires=IS_NOT_EMPTY
()),
SQLField('startdate','datetime',default=now),
SQLField('retireddate','datetime',default=now))
dk2.define_table('jlink',
SQLField('journal',dk2.journals))
dk2.define_table('jentries',
SQLField('journal',dk2.journals,requires=
[IS_NOT_EMPTY(), IS_IN_DB(dk2,'journals.id')]),
SQLField
('title',default=today,requires=IS_NOT_EMPTY()),
SQLField('content','text',requires=IS_NOT_EMPTY
()),
SQLField('createddate','datetime',default=now),
SQLField('modifieddate','datetime',default=now))
dk2.define_table('jtags',
SQLField('tag',length=40),
SQLField('jentry',dk2.jentries),
SQLField('user',dk2.jusers))
Controller:
from datetime import datetime, date, time
from gluon.sqlhtml import form_factory
today = date.today()
now = datetime.utcnow()
dk2=SQLDB("mysql://root:[email protected]:3306/dk2")
def uregister():
form = form_factory(SQLField('displayname',requires=IS_NOT_EMPTY
(),label='Display Name'),\
SQLField('username',requires=[IS_NOT_EMPTY(),IS_EMAIL
(error_message=T('Hey, this has to be an email address!')),IS_NOT_IN_DB
(dk2,'jusers.username')],label='Email Address'),\
SQLField('password','password',label='Password'))
if form.accepts(request.vars,session):
dk2.jusers.insert(displayname='form.vars.displayname',
username='form.vars.username', password='form.vars.password',
regdate='now')
response.flash="Registration Accepted"
elif form.errors:
response.flash="Sorry, there was a problem with your
Registration"
else:
response.flash="Please complete all fields before clicking the
Submit button"
return dict(form=form,vars=form.vars)
When I click on the exposed controller in the admin\design, the form
comes up fine, and when I test all the validation (e.g. NOT_IN_DB()
for the username), it all works. But, once I complete the form
correctly and submit, I get an error ticket. I've tried multiple
variations, and actually originally started off using form(), but got
the error, so switched to form_factory() instead, but still got the
same error. I thought maybe I was referencing the table too early, but
that was a problem I faced only briefly when first creating database
models in web2py.
Any other ideas?
On 2 Jan, 21:05, Robin B <[email protected]> wrote:
> As far as variable references, are dk2, dk, and db all the same
> database?
>
> There is something funny about:
>
> db(dk, 'jusers').insert(...)
>
> should it be:
>
> dk2.jusers.insert(...)
>
> Robin
>
> On Jan 2, 12:44 pm, LB22 <[email protected]> wrote:
>
> > Hi there,
>
> > I'm fairly new to both python and web2py, and I've run into a problem
> > that I just can't seem to get around. I'm hoping someone can help. If
> > anymore info is need, please let me know.
>
> > So, I keep getting this error:
>
> > Traceback (most recent call last):
> > File "gluon\restricted.pyc", line 62, in restricted
> > File "C:\Documents and Settings\Latn\Desktop\web2py_win\web2py
> > \applications\dkt/controllers/uregister.py", line 29, in <module>
> > File "gluon\globals.pyc", line 55, in <lambda>
> > File "C:\Documents and Settings\Latn\Desktop\web2py_win\web2py
> > \applications\dkt/controllers/uregister.py", line 16, in uregister
> > File "gluon\sqlhtml.pyc", line 172, in accepts
> > File "gluon\html.pyc", line 490, in accepts
> > File "gluon\html.pyc", line 136, in _traverse
> > File "gluon\html.pyc", line 136, in _traverse
> > File "gluon\html.pyc", line 136, in _traverse
> > File "gluon\html.pyc", line 136, in _traverse
> > File "gluon\html.pyc", line 139, in _traverse
> > File "gluon\html.pyc", line 366, in _validate
> > File "gluon\validators.pyc", line 183, in __call__
> > File "gluon\sql.pyc", line 300, in __getitem__
> > KeyError: 'jusers'
>
> > I don't understand why though. This is the Model for the jusers table:
>
> > dk2.define_table('jusers',
> > SQLField('displayname',length=32,requires=
> > [IS_NOT_EMPTY(),IS_LENGTH(32),]),
> > SQLField('username',length=40,requires=
> > [IS_NOT_EMPTY(),IS_EMAIL(), IS_NOT_IN_DB(dk2,'jusers.username')]),
> > SQLField('password',requires=IS_NOT_EMPTY()),
> > SQLField('regdate','datetime',default=now),
> > SQLField('retiredate','datetime',default=now),
> > SQLField('active','boolean',default=True))
>
> > And the Controller for my registration form:
>
> > def uregister():
> > form = form_factory(SQLField('displayname',requires=IS_NOT_EMPTY
> > (),label='Display Name'),\
> > SQLField('username',requires=[IS_NOT_EMPTY(),IS_EMAIL
> > (error_message=T('Hey, this has to be an email address!')),IS_NOT_IN_DB
> > (dk,'jusers.username')],label='Email Address'),\
> > SQLField('password','password',label='Password'))
>
> > if form.accepts(request.vars,session):
> > db(dk, 'jusers').insert(displayname='form.vars.displayname',
> > username='form.vars.username', password='form.vars.password',
> > regdate='now')
> > response.flash="Registration Accepted"
>
> > elif form.errors:
> > response.flash="Sorry, there was a problem with your
> > Registration"
>
> > else:
> > response.flash="Please complete all fields before clicking the
> > Submit button"
>
> > return dict(form=form,vars=form.vars)
>
> > I've wasted several hours going around in circles, hoping that I've
> > missing something really small (like on several other occasions), but
> > I've not been able to find or come up with any solutions. Any help
> > would be greatly appreciated.
>
> > Thanks
>
> > LB
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---