User_agent in a LOADed view

31 views
Skip to first unread message

Louis Amon

unread,
Jul 16, 2014, 4:15:27 PM7/16/14
to
I'm a great fan of LOAD, and i'm using it quite a bit to generate cool ajax pages.

I got one issue so far though : can't seem to cleanly detect a mobile device in a .load view.

For example I have a "result.load" view and I want a special mobile display in "result.mobile.load".

I've tried to use the decorator @mobilize but it seems that user_agent() raises an Exception when called from a LOADed view.

Is there a clean way to do that ?


For those who, like me, are struggling over this, there is a monkeypatch :

In the controller/view that calls LOAD(...):
LOAD(..., vars=dict(is_mobile=request.user_agent().is_mobile))

In the LOADed view:
is_mobile = (request.vars.is_mobile == 'True')

Anthony

unread,
Jul 16, 2014, 4:48:23 PM7/16/14
to web...@googlegroups.com
Please show your code and the exception.

Louis Amon

unread,
Jul 16, 2014, 5:06:44 PM7/16/14
to
Sure. It's fairly simple tho since the Exception gets raised on line 1 of my LOADed view.

Views/default/search.html:
...
{{ =LOAD('service', 'serv1') }}
...


Controllers/service.py:
response.view = "service.load"

def serv1():  
   
return dict()


Views/service.load :
{{ is_mobile = request.user_agent().is_mobile }}
...


Ticket :
Traceback (most recent call last):
 
File "/Users/atomos/Desktop/W2P/web2py-master/gluon/restricted.py", line 220, in restricted
 
exec ccode in environment
 
File "/Users/atomos/Desktop/W2P/web2py-master/applications/mbpd/views/service.load", line 1, in <module>
 
{{ is_mobile = request.user_agent().is_mobile }}
TypeError: 'NoneType' object is not callable

Anthony

unread,
Jul 16, 2014, 7:45:46 PM7/16/14
to web...@googlegroups.com
Note, you are using a non-Ajax component. I'm not sure why request.user_agent isn't defined in that case, but there are some funky things going on with non-Ajax components, so I would instead recommend using Ajax components:

{{=LOAD('service', 'serv1', ajax=True)}}

Feel free to file a Google Code issue about user_agent not work with non-Ajax components.

Anthony


On Wednesday, July 16, 2014 5:06:44 PM UTC-4, Louis Amon wrote:
Sure. It's fairly simple tho since the Exception gets raised on line 1 of my LOADed view.

Views/default/search.html:
...
{{ =LOAD('service', 'serv1') }}
...


Controllers/service.py:
response.view = "service.load"

def serv1():  
   
return dict()


Views/service.load :
{{ is_mobile = request.user_agent().is_mobile }}
...


Ticket :
Traceback (most recent call last):
 
File "/Users/atomos/Desktop/W2P/web2py-master/gluon/restricted.py", line 220, in restricted
 
exec ccode in environment
 
File "/Users/atomos/Desktop/W2P/web2py-master/applications/mbpd/views/service.load", line 1, in <module>
 
{{ is_mobile = request.user_agent().is_mobile }}
TypeError: 'NoneType' object is not callable


On Wednesday, July 16, 2014 10:48:23 PM UTC+2, Anthony wrote:
Reply all
Reply to author
Forward
0 new messages