Login

iPhone Redirect Middleware

Author:
myles
Posted:
March 18, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

I didn't really like the current state of iPhone/Mobile redirect middleware mainly because I wanted something that was closer to twitters use case. So I came up with this. I don't think it a great snippet and I will probably fix it in the near future. But it works.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re

from django.http import HttpResponseRedirect
from django.conf import settings
from django.contrib.sites.models import Site

class iPhoneMiddleware(object):
        def __init__(self):
                self.main_site = Site.objects.get(pk=1)
                self.mobile_site = Site.objects.get(pk=2)

        def process_request(self, request):
                if not request.session.get('mobile'):
                        p = re.compile('iPhone|iPod', re.IGNORECASE)
                        if p.search(request.META['HTTP_USER_AGENT']):
                                request.session['mobile'] = True
                        else:
                                request.session['mobile'] = False

                        return

                if not self.mobile_site == Site.objects.get_current():
                        if request.session.get('mobile'):
                                return HttpResponseRedirect('http://%s%s' % (self.mobile_site.domain, request.path))

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.