Login

Detect iPhone & Switch Template via render_to_response

Author:
bryanhelmig
Posted:
May 23, 2010
Language:
Python
Version:
1.2
Tags:
iphone template render_to_response
Score:
-2 (after 2 ratings)

A fast way to implement an iPhone template switcher, especially if you have a lot of existing views using the render_to_response() shortcut. This checks for the iPhone browser and then modifies the chosen template by adding -mobile to the html's file name.

Check out this more complete list of user agents if you need to detect specific mobile devices.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django.shortcuts import render_to_response as render_to_response_pre

def render_to_response(template, context):
    '''
    If the exisiting view uses a template named foo.html, any iPhone
    will trigger the template named foo-iphone.html.
    '''
    if 'HTTP_USER_AGENT' in context['request'].META:
        if 'iphone' in context['request'].META['HTTP_USER_AGENT'].lower():
            template = template.rstrip('.html')
            template += '-iphone.html'
    return render_to_response_pre(template, context)

More like this

  1. Mobile browser detection middleware by pavl 5 years ago
  2. Simple Mobile Support by bahoo 4 years, 7 months ago
  3. iPhoneMiddleware by henriklied 6 years, 7 months ago
  4. Mobile Device Middleware by stephen_mcd 5 years ago
  5. mini_render_to_response by menendez 6 years, 1 month ago

Comments

Please login first before commenting.