Yibing Liu
Yibing Liu

Reputation: 21

[email protected]() error: 'NoneType' object has no attribute

I have add code in my db.py to add extra field before the auth.define_tables(username=False, signature=True):

auth.settings.extra_fields['auth_user'] = (
    [Field('user_type',label="reg_type",default="Stu", requires=IS_IN_SET(['Stu, 'Com'],multiple=False))])

and I want to do some authentication in my action like the following:

@auth.requires(auth.user.user_type=="Stu",requires_login=True)
define someaction:
    code

However I get a error ticket when I run my application.

'NoneType' object has no attribute 'user_type'

And I have read the manual book

@auth.requires also takes an optional argument requires_login which defaults to True. If set to False, it does not require login before evaluating the condition as true/false. The condition can be a boolean value or a function evaluating to boolean.

Note that access to all functions apart from the first one is restricted based on permissions that the visitor may or may not have.

If the visitor is not logged in, then the permission cannot be checked; the visitor is redirected to the login page and then back to the page that requires permissions.

So I can't understand what results in the error. Thank you.

Upvotes: 0

Views: 599

Answers (1)

Anthony
Anthony

Reputation: 25536

When the user is not logged in, auth.user is None. So, you must account for that in your condition:

@auth.requires(auth.user and auth.user.user_type == "Stu")

Note, requires_login=True is the default, so you don't have to specify that (and in this case, it would be unnecessary either way, as auth.user not being None implies the user is logged in).

Upvotes: 1

Related Questions