The username field is used in case you don't want users to log in with
their email address. If you want the username field to store the user's
email address, then just stick with the standard email field and forget
about the username field.
Anthony
On Thursday, May 31, 2012 4:09:39 PM UTC-4, Osman Masood wrote:
>
> Hi,
> I have a custom table, and I'm trying to use a 'username' field instead of
> an 'email' field (since the book says if there's a username field, it will
> be used instead of the email field).
> However, whenever I try the 'forgot my username' function, it gives me an
> error, saying:
> <type 'exceptions.KeyError'> 'email'
> (Looks like it's trying to access the email field, which of course doesn't
> exist.)
>
> Here's my code:
> auth.settings.table_user_name = 'user'
> db.define_table(auth.settings.table_user_name,
> Field('username', length=60, unique=True, label='Email address'), #
> required
> Field('password', 'password', length=512, writable=False,
> readable=False, label='Password'), # required
> Field('registration_key', length=512, writable=False, readable=False,
> default=''), # required
> Field('reset_password_key', length=512, writable=False,
> readable=False, default=''), # required
> Field('registration_id', length=512, writable=False, readable=False,
> default=''), # required
> format='%(username)s')
>
> ## do not forget validators
> custom_auth_table = db[auth.settings.table_user_name] # get the
> custom_auth_table
> custom_auth_table.password.requires = [ CRYPT(key="mykey",
> digest_alg="sha512") ]
> custom_auth_table.username.requires = [
> IS_EMAIL(error_message=auth.messages.invalid_email),
> IS_NOT_IN_DB(db, custom_auth_table.username)] # we need this so that
> web2py uses username as the email address. unique is not good enough
> auth.settings.table_user = custom_auth_table # tell auth to use
> custom_auth_table
>
> ## configure auth policy
> auth.settings.registration_requires_verification = True
> auth.settings.registration_requires_approval = True
> auth.settings.reset_password_requires_verification = True
> auth.messages.verify_email = 'Click on the link http://' + \
> request.env.http_host + \
> URL(r=request,c='default',f='user',args=['verify_email']) + \
> '/%(key)s to verify your email'
> auth.messages.reset_password = 'Click on the link http://' + \
> request.env.http_host + \
> URL(r=request,c='default',f='user',args=['reset_password']) + \
> '/%(key)s to reset your password'
>
> auth.define_tables(username=True, signature=False, migrate=False)
>
> ## configure email
> mail=auth.settings.mailer
> mail.settings.server = 'smtp.gmail.com:587' or 'logging'
> mail.settings.sender = '[email protected]'
> mail.settings.login = '[email protected]:mypass'
>
>
> And here's the error traceback:
>
> Traceback (most recent call last):
> File "/home/www-data/web2py/gluon/restricted.py", line 205, in restricted
> exec ccode in environment
> File "/home/www-data/web2py/applications/Talent_Web/controllers/default.py"
> <https://ec2-204-236-184-137.us-west-1.compute.amazonaws.com/admin/default/edit/Talent_Web/controllers/default.py>,
> line 73, in <module>
> File "/home/www-data/web2py/gluon/globals.py", line 175, in <lambda>
> self._caller = lambda f: f()
> File "/home/www-data/web2py/applications/Talent_Web/controllers/default.py"
> <https://ec2-204-236-184-137.us-west-1.compute.amazonaws.com/admin/default/edit/Talent_Web/controllers/default.py>,
> line 34, in user
> return dict(form=auth())
> File "/home/www-data/web2py/gluon/tools.py", line 1196, in __call__
> return getattr(self,args[0])()
> File "/home/www-data/web2py/gluon/tools.py", line 2158, in retrieve_username
> old_requires = table_user.email.requires
> File "/home/www-data/web2py/gluon/dal.py", line 7180, in __getattr__
> return self[key]
> File "/home/www-data/web2py/gluon/dal.py", line 7120, in __getitem__
> return dict.__getitem__(self, str(key))
> KeyError: 'email'
>
>