When web2py writes A('my link', _href=URL(r=request, vars=request.vars)) to 
HTML, it results in:
 
<a href="/page?input_one=aaa&reg_no=bbb">my link</a>
 
The problem is, the *browser* converts the "&reg" in the href to the 
registered trademark symbol, so when you click the link, the requested URL 
sent to web2py ends up including the trademark symbol in place of the "&reg" 
portion of the URL (presumably, this would happen with any HTML entity, such 
as &copy, etc.).
 
To avoid this kind of problem, I think href attributes are supposed to 
include "&amp;" in place of "&", so the link should have been written as:
 
<a href="/page?input_one=aaa&amp;reg_no=bbb">my link</a>
 
So, why doesn't web2py write the URL properly? When a helper (such as the 
A() helper) is converted to HTML, web2py passes its attributes to the 
xmlescape() function, which calls cgi.escape(), which automatically converts 
"&" to "&amp;". However, the xmlescape() function does not escape objects 
that have an 'xml' attribute, and because the URL() function returns an 
object with an 'xml' attribute, it is not escaped.
 
This should probably be fixed somehow, but for now, a workaround is to 
explicitly call the 'xml' method of URL, which will convert it to a string 
before it is passed to xmlescape(). So, change your code to:
 
link = A('my link', _href=URL(r=request, vars=request.vars).xml()) 
 
Note, this issue is not a problem if you manually enter an href that 
includes an HTML entity. For example:
 
link = A('my link', _href='/page?input_one=aaa&reg_no=bbb')
 
will properly be converted to:
 
<a href="/page?input_one=aaa&amp;reg_no=bbb">my link</a>
 
Best,
Anthony
 

On Tuesday, March 29, 2011 9:53:50 AM UTC-4, Jim Karsten wrote:

> I have a variable named 'reg_no' that when used as a link paramater is 
> getting converted into a registered trademark symbol. Here is a simple 
> example that illustrates it. 
>
> The controller: 
>
>     def page(): 
>         link = A('my link', _href=URL(r=request, vars=request.vars)) 
>         form = SQLFORM.factory( 
>             Field('input_one', default=request.vars.input_one), 
>             Field('reg_no', default=request.vars.reg_no), 
>             ) 
>         return dict(form=form, link=link) 
>
> The view: 
>
>     {{=form}} 
>     {{=link}} 
>
>
> Enter values in both fields, then submit. Then click the link. The 
> link should refresh the page, with the form populated with the entered 
> values. If the second field is named 'input_two' it works. But when 
> it's named 'reg_no' the first input has 'aaaR_no=bbb' where 'aaa' and 
> 'bbb' are the entered values and R is the registered trademark symbol, 
> and the second input is blank. 
>
> The link url is very different depending on the field name: 
>
> 'input_two': http://www...com/demo/default/page?input_one=aaa&input_two=bbb 
>
> 'reg_no'   : http://www...com/demo/default/page?input_one=aaa%C2%AE_no%3Dbbb 
>
>
> I renamed 'reg_no' to something not starting with 'reg_' and I have it 
> working. I'm not sure if this is a bug or not. 
>
> Any insight would be welcome. 
>
> Jim Karsten 
>

Reply via email to