# Save RAM with mobile middleware
A while back I wrote an article on how to [set up a mobile site with Django](/posts/2008/feb/18/going-mobile/). Currently I have a [Slicehost](http://www.slicehost.com) account which includes 256MB of RAM. My resources are tight and I really dislike having another set of unnecessary Apache processes for a mobile site that, aside from different templates, is using the same code base. The solution is quite simple, write a middleware.
The following code checks the incoming request for 'm' or 'mobile' in the domain name. If it exists the
TEMPLATE_DIRS is replaced by a
def process_request(self, request):
domain = request.META.get('HTTP_HOST', '').split('.')
if 'm' in domain or 'mobile' in domain:
settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIRS
settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIRS
You'll notice this requires you to also have a
DESKTOP_TEMPLATE_DIRS (or whatever you want to call it) so you can switch back to the desktop version.
If you're using any sort of caching you'll want be sure and change
CACHE_MIDDLEWARE_KEY_PREFIX when you change to mobile templates and back again.
You'll probably want to place this middleware after Session and Authentication Middleware and before the Cache Middleware like so:
MIDDLEWARE_CLASSES = (
Go Green! Save RAM! (kidding)