If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

detect_mobile_browser returns True if the browser is mobile. If it is, the view is set to function.mobile.extension.

For example, if we have this request: http://example.com/user/view/bob, the mobile view would be located in views/user/view.mobile.html

If the mobile view file doesn't exist, we end up with the regular view: views/user/view.html

This slice of code requires a couple of Python modules: mobile.sniffer and PyWURFL. Both can be installed via pip or easy_install.

I have the following code in models/0.py

# Detect mobile device.  Set to mobile view if available

from mobile.sniffer.detect import detect_mobile_browser
if detect_mobile_browser(request.env.http_user_agent):
    mobile_view = '%s.mobile.%s' % (request.function, request.extension)
    if os.path.exists(os.path.join(request.folder, 'views',request.controller, mobile_view)):
        response.view = '%s/%s' % (request.controller, mobile_view)

Comments (0)

Hosting graciously provided by:
Python Anywhere