This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

What is this?

This library is a collection of middleware and decorators that help in creating mobile views and directing users to the mobile version of your site. It makes these assumptions:

  • You can use Vary: User-Agent to serve mobile and non-mobile content through the same URLs.
  • You want to use separate views and/or templates for the mobile site. If you’re building a mobile experience through media queries this library won’t be helpful.
  • Not all views from the normal site need to be replaced with mobile views.


Written and tested on Django trunk with Python 2.6.


These are the default settings:

# A regex for detecting mobile user agents.
MOBILE_USER_AGENTS = 'android|fennec|iemobile|iphone|opera (?:mini|mobi)'
# The name of the cookie to set if the user prefers the mobile site.
MOBILE_COOKIE = 'mobile'

You need these middleware (but see the User Agent caveats below):


How the Mobile Site is Chosen

  1. The HTTP_USER_AGENT matches MOBILE_USER_AGENTS and the MOBILE_COOKIE is not set to off.
  2. or the MOBILE_COOKIE is set to on.
  3. A mobile view exists for the current URL.

The HTTP_USER_AGENT is checked against the regular expression in MOBILE_USER_AGENTS. The default is a very basic set of user agents to ease maintenance and because the cookie provides a fallback.

If MOBILE_COOKIE is set to on, through Set-Cookie or through javascript, the mobile site will be chosen regardless of the user agent. If MOBILE_COOKIE is set to off the normal site will always be chosen.

Changes to the request Object

If the current request is for the mobile site, request.MOBILE = True. At all other times request.MOBILE = False.


Some decorators are provided to assist with common idioms:

def view(request, template=None):

@mobile_template helps with the pattern of using the same view code and template context, but switching to a different template for mobile. It follows this logic:

template = 'app/mobile/detail.html' if request.MOBILE else 'app/detail.html'

To use a completely different function for the mobile view:

def view(request):

def view(request):

In the example, the first definition of view will be used for the normal site and the second function will be used for the mobile site. The normal and mobile site point to the same view in and the decorator handles choosing which view to run.

Varying on User Agent

Since mobile users can enter the site from any normal URL, the DetectMobileMiddleware always inspects the User-Agent to see if it matches something in MOBILE_USER_AGENTS, and may redirect the browser to the mobile site. Thus, every URL on the site should be sending Vary: User-Agent to get proper HTTP caching. Varying on User-Agent can be detrimental to your frontend cache scheme, so it’s recommended that you move mobile detection up the stack to a frontend proxy.

The proxy can run the logic in DetectMobileMiddleware and set HTTP_X_MOBILE (so we know whether to serve a mobile view) without varying on user agent internally. Instead, it can vary on X-Mobile while sending Vary: User-Agent back to the client. From the outside it looks like the app varies on User-Agent but the proxy will only need to cache a mobile and non-mobile version of the URL.

Release History

Release History


This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-mobility-0.1.tar.gz (5.9 kB) Copy SHA256 Checksum SHA256 Source Feb 3, 2011

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting