auth.user question

169 views
Skip to first unread message

Mark Finkelstein

unread,
Aug 4, 2013, 10:10:09 AM8/4/13
to web...@googlegroups.com
I have two lines of code that read as follows:

if auth.user:
   
if db.auth_user[auth.user.id]['image']=='' or db.auth_user[auth.user.id]['image']==None:

Recently, while logged in, I dropped all the tables, refreshed the page and then got an Internal Error saying

 if db.auth_user[auth.user.id]['image']=='' or db.auth_user[auth.user.id]['image']==None:
TypeError: 'NoneType' object has no attribute '__getitem__'

I'm guessing it has to do with cookies saving the session, where the web2py side recognizes what seems to be a session and building auth.user based off of this, but I'm not quite sure why auth.user is built without a query check of the database? I was wondering why this is?

Massimo Di Pierro

unread,
Aug 4, 2013, 10:35:14 AM8/4/13
to web...@googlegroups.com
If you are not logged in db.auth_user[auth.user.id] is None.

Mark Finkelstein

unread,
Aug 4, 2013, 12:04:16 PM8/4/13
to web...@googlegroups.com
I get  the following error for trying that:
print db.auth_user[auth.user.id]
AttributeError: 'NoneType' object has no attribute 'id'

I thought `auth.user` is supposed to be used to check if the user is logged in?

Anthony

unread,
Aug 4, 2013, 3:07:46 PM8/4/13
to web...@googlegroups.com
If the user is not logged in, auth.user will return None, so you can't do auth.user.id. Instead, you should use auth.user_id, which will return None if not logged in.

Anthony

Mark Finkelstein

unread,
Aug 4, 2013, 4:58:40 PM8/4/13
to web...@googlegroups.com
Yes, I thought this was so, my issue is more with the fact that after dropping the databases, this seems to give an invalid value, as shown in my first post? I may be wrong.

Anthony

unread,
Aug 4, 2013, 7:27:31 PM8/4/13
to web...@googlegroups.com
auth.user is stored in the session and is not refreshed from the database on every request, which would be inefficient. Presumably you don't typically drop your db tables in production.

Anthony

Mark Finkelstein

unread,
Aug 4, 2013, 8:39:13 PM8/4/13
to web...@googlegroups.com
This makes sense. This was what I was wondering. Thanks for the answer!


--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/GlGgNaNN7ZI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages