Created
September 21, 2012 03:24
-
-
Save ckinsey/3759561 to your computer and use it in GitHub Desktop.
Mobile middleware success!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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