Note, the signature of the URL() function is URL(a, c, f, r, args, vars,
...). So, in your first example, it interprets "addressbook" as the
application and request.args(0) as the function, and in your second
example, it interprets request.args(0) as the request object (which is why
you get the error about a str object not having an "application"
attribute). So, in general, it is best to specify "args" and "vars" as
keyword arguments -- if you want to specify them as positional arguments,
you have to fill in all four preceding positional arguments as well.
Also, note that regarding the first three arguments, URL() is smart -- if
you only specify two positional arguments, it assumes they are c and f
rather than a and c, and if you only specify one positional argument, it
assumes it is f rather than either a or c (if a and or c are missing, they
get filled in with the current application and controller).
Anthony
On Tuesday, August 14, 2012 4:14:20 AM UTC-4, Annet wrote:
>
> As part of a router function I have the following code:
>
> id=request.args(0)
> account=db(db.NodeAccount.nodeID==id).select(db.NodeAccount.ALL).first()
> if account:
> if account.statusID!=1: # in that case the account is blocked or under
> maintenance
> redirect(URL('card',args=id))
> elif account.accountID==ADVANCEDACCOUNTID:
> if not session[id]:
> session[id]=Storage(id=id)
> session[id].accountid=ADVANCEDACCOUNTID
> redirect(URL('site','index',args=id))
> elif account.accountID==ADVANCEDCONNECTORACCOUNTID:
> if not session[id]:
> session[id]=Storage(id=id)
> session[id].accountid=ADVANCEDCONNECTORACCOUNTID
> redirect(URL('connector','index',args=id))
>
> In a controller I start the index function with the following code:
>
> if not len(request.args):
> redirect(URL('addressbook','router'))
> elif not session[request.args(0)]:
> redirect(URL('addressbook','router',request.args(0)))
> elif not session[request.args(0)].accountid==ADVANCEDACCOUNTID:
> redirect(URL('addressbook','router',request.args(0)))
> else:
> ....
>
> When I have an object session[283] and visit the url:
> http://127.0.0.1:8000/bootstrap/site/index/283 and then change 283 to
> 2053: http://127.0.0.1:8000/bootstrap/site/index/2053
> (and session[2053] doesn't exist) instead of a redirection to
> http://127.0.0.1:8000/bootstrap/addressbook/router/2053 I get an invalid
> request: http://127.0.0.1:8000/addressbook/router/2053
>
> When I add the application to the url:
> redirect(URL('bootstrap','addressbook','router',request.args(0))), I get:
>
> Traceback (most recent call last):
> File "/Library/Python/2.5/site-packages/web2py/gluon/restricted.py", line
> 205, in restricted
> exec ccode in environment
> File
> "/Library/Python/2.5/site-packages/web2py/applications/bootstrap/controllers/site.py"
> <http://127.0.0.1:8000/admin/default/edit/bootstrap/controllers/site.py>,
> line 192, in <module>
> File "/Library/Python/2.5/site-packages/web2py/gluon/globals.py", line 173,
> in <lambda>
> self._caller = lambda f: f()
> File
> "/Library/Python/2.5/site-packages/web2py/applications/bootstrap/controllers/site.py"
> <http://127.0.0.1:8000/admin/default/edit/bootstrap/controllers/site.py>,
> line 19, in index
> redirect(URL('bootstrap','addressbook','router',request.args(0)))
> File "/Library/Python/2.5/site-packages/web2py/gluon/html.py", line 246, in
> URL
> application = r.application
> AttributeError: 'str' object has no attribute 'application'
>
>
> Why doesn't this work?
>
> Kind regards,
>
> Annet.
>
>
>
--