Skip to content

Instantly share code, notes, and snippets.

@ckinsey
Created September 21, 2012 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckinsey/3759561 to your computer and use it in GitHub Desktop.
Save ckinsey/3759561 to your computer and use it in GitHub Desktop.
Mobile middleware success!
from django.conf import settings
class MobileTemplateMiddleware:
def process_request(self, request):
host = request.META.get('HTTP_HOST', '')
host_s = host.split('.')
if len(host_s) > 1:
subdomain = host_s[0]
else:
subdomain = None
# This is where the magic happens
if subdomain == settings.MOBILE_SUBDOMAIN:
settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIR + settings.TEMPLATE_DIRS
request.subdomain = subdomain
request.host = host
###
### The relevant settings:
###
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
)
import os.path
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))# this is not Django setting.
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, "templates"),
)
MOBILE_SUBDOMAIN = 'm'
MOBILE_TEMPLATE_DIR = os.path.join(PROJECT_PATH, "mobile_templates"),
MIDDLEWARE_CLASSES = (
#...
'middlewaretest.web.middleware.MobileTemplateMiddleware',
#...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment