The user() function is called with an additional argument in the URL (e.g.,
/user/login, /user/register, etc.). This extra argument is accessible via
request.args[0]. Now, the Auth class has a __call__ method, so the auth
object is callable. When you call it via auth(), the __call__ method
essentially acts like a router -- it reads request.args[0] and then calls
whatever Auth method is specified (or it redirects to /user/login if there
is no request.args[0]). You can see this
here: http://code.google.com/p/web2py/source/browse/gluon/tools.py#1143. In
particular:
return getattr(self,args[0])()
gets the Auth method named in args[0] and calls that method.
Note, you can also manually create your own actions (or do your own routing
based on request.args) and call the individual Auth methods directly. For
example, you could create a dedicated "register" action via:
def register():
return dict(form=auth.register())
Crud works in a similar way.
Anthony
On Thursday, February 23, 2012 8:34:47 PM UTC-5, davidkw wrote:
>
> I'm reading through the tutorial and just came across the part that
> adds the code:
>
> from gluon.tools import Auth
> auth = Auth(db)
> auth.define_tables()
>
> def user():
> return dict(form=auth())
>
> And suddenly login, register, etc were enabled. This is the first
> "magic" I've come across in web2py that I don't understand well. I
> understand that auth.define_tables() creates the tables for the user
> data, but I'm not sure how def user():... suddenly adds all the
> registration and login pages.
>
> Could someone give me a quick walkthrough of what's going on?