views:

111

answers:

5

I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.

Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.

+8  A: 

Have two sets of templates, one for mobile, one for desktop. Store the filenames in a pair of dictionaries, and use the User-agent header to detect which set should be used. Also allow manual selection of which site to use via a session entry.

Ignacio Vazquez-Abrams
+1  A: 

If you place a class on your body (Django uses something similar to specify what column style to use), you could use the same templates but simply use different stylesheets. I'm not sure what main differences you are using separate templates for, but this might allow you to cut down on re-coding the templates multiple times.

Adam
A: 

There are different strategies.

If you've a lot of views that renders to template files for the web version, and don't want to rewrite all views checking if the request is coming from a mobile user-agent, you'd be better writing a Middleware.

A workflow could be like this:

def process request:
  if from_mobile:
    settings.TEMPLATE_DIRS=settings.TEMPLATE_MOBILE_DIRS
  else:
    settings.TEMPLATE_DIRS=settings.TEMPLATE_WEB_DIRS

There is only a little problem here: As Django Documentation reports, it's not correct to change settings at runtime: http://docs.djangoproject.com/en/dev/topics/settings/#altering-settings-at-runtime

So you may want to call

django.conf.settings.configure(default_settings, **settings)
Simone
A: 

The answer depends heavily on the type of your target audience. If you target for modern mobile browsers equivalents to their desktop counterparts (such as WebKit-based), all you need is specific stylesheet with appropriate media query (you are basically designing for low-res rather than mobile).

Totally different strategy is needed if your site (e.g. airline schedules) must to be accessible widest possible range of mobile devices, some of running very old / limited browsers. Then custom (html) templates may be easiest way to go.

jholster
A: 

You might want to check out mobilesniffer and django-bloom to see if they fit your purposes.

bebraw