Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Clone from Google Code: Django middleware and view decorator to detect phones and small-screen devices
Python
branch: master

This branch is 9 commits ahead of saschwarz:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
Experiments
src/minidetector
.gitignore
MANIFEST.in
__init__.py
readme.markdown
setup.py

readme.markdown

Info

This application is a simple middleware and associated decorator that will add a ".mobile" attribute to your request objects, which, if True, means the requester is coming to you from a mobile phone (cellphone), PDA, or other device that should be considered small screened, or have an underpowered browser, such as games consoles.

This mostly works using a list of search strings, though there are a couple of other tricks, like detecting the presence of Opera Mini. The strings are in an easily-parseable text file, and thus can be used for other similar projects.

It also includes a pretty extensive list of user agents to test against.

If sessions are enabled subsequent requests obtain mobile attributes from the session without parsing the requestor's meta data, unless MINIDETECTOR_USE_SESSIONS is set to False in settings.py.

Optionally "mobile" requestors can be redirected to a mobile domain. If they come back to the non-mobile domain the presence of session data keeps them from be automatically redirected back to the mobile domain.

Adapted from http://code.google.com/p/minidetector/

Installation

Using minidetector is very simple. Simply place the minidetector package into your project's path, and then add:

minidetector.Middleware

to your MIDDLEWARE_CLASSES tuple in your settings.py

Then in your view you can check request.mobile - if it's True then treat it like a small screen device. If it's False then it's probably a desktop browser, or a spider or something else.

If you only have certain views that need the distinction, you can choose not to search every request you receive. All you need to do is wrap the relevant views like this:

from minidetector import detect_mobile

@detect_mobile
def my_mobile_view(request):
    if request.mobile:
        #do something with mobile

"Special" Devices

minidetector also adds some extra info for popular devices to the request object. The defaults for a desktop browser:

- request.wap = False
- request.browser_is_android = False
- request.browser_is_ios = False
- request.browser_is_ipad = False
- request.browser_is_iphone = False
- request.browser_is_webkit = False
- request.mobile_device = ''
- request.touch_device = False
- request.wide_device = True

Extra Info

for webkit:

request.browser_is_webkit = True

for the iPad:

request.browser_is_ipad = True
request.mobile_device = 'ipad'

for the iPhone or iPod touch:

request.browser_is_iphone = True
request.mobile_device = 'iphone'

for iOS in general:

request.browser_is_ios = True

and for Android:

request.browser_is_android = True
request.mobile_device = 'android'

I also send 'request.touch_device' as True for all webkit touch devices (iOS and android.)

I also now send 'request.wide_device' as True for all 'wide' devices. Currently this covers all desktops and the ipad. I'll revisit this when other tablets (webOS or android) come out (if I can detect it.)

Override mobile treatment

By default the iPad is not treated as a mobile browser. If you want to reverse this, add the following to your settings.py:

IPAD_IS_MOBILE = True

You can do the reverse for iphone and android (i.e. having them treat your site as non-mobile.)

IPHONE_IS_MOBILE = False
ANDROID_IS_MOBILE = False

Redirect to mobile domain

By default only the request is modified as described above. If MOBILE_URL is set to a non empty string and the request.mobile is True the requestor will be redirected to the MOBILE_URL. The MOBILE_URL setting can contain python format string substitutions (using the .format() string method) --- the variables path (request path without query string, such as /page'),full_path(with query string, such as/page?id=5andhost(HTTP host, such asexample.com) are all available, so if you want to preserve the request path, setMOBILE_URLto a path such ashttp://mobile.example.com/{full_path}`

If sessions are enabled and a requestor returns to the non mobile site they will not be redirected again to the MOBILE_URL site (unless MINIDETECTOR_USE_SESSIONS is set to False).

This also means that mobile clients that revisit the non-mobile site on a later visit while their session still exists on the server won't be redirected to the mobile version of the site. This behavior can be changed by setting SESSION_EXPIRE_AT_BROWSER_CLOSE to True or by setting SESSION_COOKIE_AGE to an appropriately short time.

It is also recommended that mobile and non-mobile versions of the sites share the same SESSION_COOKIE_DOMAIN so the sessions are shared between sites: SESSION_COOKIE_DOMAIN = '.example.com'

Something went wrong with that request. Please try again.