Strange 'SQLTABLE linkto' behaviour

63 views
Skip to first unread message

mcamel

unread,
Feb 20, 2014, 5:17:47 AM2/20/14
to web...@googlegroups.com
Hi,

I've seen an unexpected 'SQLTABLE linkto' behaviour (both at 2.8.2 and trunk version):

In a controller:

def sqltablelinkto():
   
    db = DAL('sqlite:memory:')
    db.define_table('parent',Field('name'))
    db.parent.insert(name='Parent1')
    db.parent.insert(name='Parent2')
   
    l
= URL('')
   
   
return CAT(H5('request.args=%s' % request.args),
               H5
('request.vars=%s' % request.vars),
               SQLTABLE
(db(db.parent).select(), linkto=l))

This works as expected when you click at the id links: name of table and id of record are passed to args, and nothing to vars. URLs are formed like this: http://.../sqltablelinkto/parent/1

But if you want to pass something in vars, things get weird.
Change the url line to:

l = URL('', vars=dict(data='data in vars'))

Now, when you click at the id links, you get NOTHING at request.args, and all the info is mixed up at request.vars: 'data': 'data in vars/parent/1'

The formed url shows the reason: http://.../sqltablelinkto?data=data+in+vars/parent/1
I expected this url to be formed: http://.../sqltablelinkto/parent/1?data=data+in+vars
so you'll get args and vars stored properly.

Am i doing something wrong or should i open a bug ticket?.

Thanks!

mcamel

unread,
Feb 25, 2014, 11:37:21 AM2/25/14
to web...@googlegroups.com

Massimo Di Pierro

unread,
Feb 25, 2014, 12:26:49 PM2/25/14
to web...@googlegroups.com
Why are you passing an empty string as action? 

= URL('', vars=dict(data='data in vars'))

Perhaps you want:

= URL(args=request.args, vars=dict(data='data in vars'))

mcamel

unread,
Feb 25, 2014, 3:32:09 PM2/25/14
to web...@googlegroups.com
Just to point to the same function in order to make an autocontained example.

I don't use empty string in my application. Anyway i've tested your suggestion and gives the same result.

Massimo Di Pierro

unread,
Feb 25, 2014, 5:30:14 PM2/25/14
to web...@googlegroups.com
Now I understand better. You cannot pass vars to linkto. That is by design because in principle, the linkto function may need parameters specified by the SQLFORM.


On Thursday, 20 February 2014 04:17:47 UTC-6, mcamel wrote:

mcamel

unread,
Feb 26, 2014, 4:44:21 AM2/26/14
to web...@googlegroups.com
Looking at gluon/sqltml.py code i think i realized how 'linkto' works:
  • is a lambda is passed, it's used to generate the href
  • if not, a string is concatenated: href = '%s/%s/%s' % (linkto, tablename, r_old)
The manual says nothing about expecting a lambda, so i figured out you have to pass an URL and after that, 'tablename' and 'r_old' were appended automatically to the args part. But it's not that way: if you do not pass a lambda, you only can pass the name of a function (and then 'tablename' and 'r_old' are appended at the end).

Knowing that, this code seems to do the trick (replacing the empty string with the name of the function to redirect):

l = lambda field, type, ref: URL(f='', args=[ref, field], vars=dict(data='data in vars'))

If you agree you can close 1881 ticket. It's not a bug at the code (maybe at the manual).

By the way, is it proper to open issues to suggest corrections of the manual (i have a few)?.

Thanks.

Massimo Di Pierro

unread,
Feb 26, 2014, 9:09:22 AM2/26/14
to web...@googlegroups.com
Yes. I had forgotten about the lambda option. :-)

LightDot

unread,
Feb 26, 2014, 3:13:01 PM2/26/14
to web...@googlegroups.com


On Wednesday, February 26, 2014 10:44:21 AM UTC+1, mcamel wrote:
...

If you agree you can close 1881 ticket. It's not a bug at the code (maybe at the manual).

By the way, is it proper to open issues to suggest corrections of the manual (i have a few)?.

Thanks.

You can also open an issue or make a pull request at the book's github repo:

https://github.com/mdipierro/web2py-book

Regards

mcamel

unread,
Feb 27, 2014, 5:58:04 AM2/27/14
to web...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages