{{user = db.auth_user(username=myuser)  # should probably move this to the 
controller}}
{{='%(first_name)s %(last_name)s' % user if user else 'user not a member of 
site'}}

Note, there is no reason to use two separate queries to get the first and 
last name, and there is no reason to use the .as_list() method. Instead, 
the full query would be:

db(db.auth_user.username == myuser).select(db.auth_user.first_name, db.
auth_user.last_name).first()

The .first() method extracts the first returned record or yields None if 
there are no matching records. The above db.auth_user(username=myuser) is 
just a shortcut for the same thing.

Anthony

On Saturday, August 10, 2013 8:03:49 PM UTC-4, Rob_McC wrote:
>
>
>    - This code is working, but...
>    - is there a better or shorter way to accomplish this.
>    
>
> ~ thanks for help.
> Rob
>
>
>
> {{
>
> # QUESTION:
> # is there a more direct way of accomplishing this?
>
> # file: userinfo.html  (a web2py view)
>
> # I'm logged in as
> # user: jdoe
>
> # this will return first_name and last_name of any OTHER user in 
> db.auth_user
> # if they exist
>
>
> # I want to access information on ANOTHER user: jsmith
> # jdoe will type in to a form (not shown here):
> # jsmith
> # to see if they are a user.
>
>
> # db.auth_user holds:
> #  username: jsmith
> #  first_name: John
> #  last_name: Smith
>
>
> # just use variable myuser for this example snippet
> myuser = 'jsmith'
>
> # attempt to grap the first and last name of user jsmith
>
> first =  db(db.auth_user.username == myuser).select(db.auth_user.
> first_name).as_list()
> second = db(db.auth_user.username == myuser).select(db.auth_user.last_name
> ).as_list()
>
> # see if the query returned anything. i.e. test if username is in database
> if len(first)>0:
>     s1=first[0]['first_name']
>     s2 =second[0]['last_name']
>
>     # prints:   John Smith
>     = s1 + ' ' + s2
>     else:
>     = "user not a member of site"
> pass
>
> }}
>
>
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to