Acknowledging the Mobile Web with Django

KTKA breaking news homepageI was reading up on HowToProvideAlternateViewsForMobileDevices on the Rails wiki this morning and couldn’t help but notice how much easier it is to set up a mobile version of a Django site. At World Online we have stripped-down barebones no frills “all we want are the facts ma’am” versions of all of our sites. They prove extremely useful during KU basketball games or when you’re in downtown lawrence and want to know what restaurants are open. Since our mobile sites are just alternate templates on the same views, setup goes something like this:

In main_site.settings:

TEMPLATE_DIRS = (
'/path/to/templates/mainsite.com/',
'/path/to/templates/default/',
)

In mobile_site.settings:

from main_site.settings import *
TEMPLATE_DIRS = (
'/path/to/templates/mobile.mainsite.com/',
'/path/to/templates/default/'
) 

The first line imports all of the settings from your main site. We then overwrite the TEMPLATE_DIRS setting to point to the mobile version of our templates (and fall back to default templates if there isn’t a mobile specific version). Because every app that we write also gets a default template we can have a complete mobile site up and running by creating just one or two mobile base templates.

While Django can’t help you debate internally the “one web” versus “two webs” philosophies, it can definitely help you produce lightweight mobile-friendly content with minimum effort.

7 Responses to “Acknowledging the Mobile Web with Django”


  1. 1 Joseph Rose Jul 26th, 2006 at 10:03 am

    Wow! This sounds really simple!

    Does Django automatically detect when to use the mobile_site.settings file, or did you configure this somewhere else?

  2. 2 Matt Croydon Jul 26th, 2006 at 10:13 am

    Joseph,

    We’ve done our best to promote our mobile sites on our “regular” by promoting mobile.sitename.com. Hopefully our users can expect to find a site there and it’s consistent across our different sites so that mobile.sitename.com is “guessable”.

    Automatically selecting which type of content to serve up based on device, browser, capabilities, etc is *really* hard to get right. If you’d like to go that route, you might want to check out WURFL which is about as good as it gets for detecting mobile device capabilities.

    I’d definitely suggest checking out Brian Fling’s mobile design slides from WebVisions 2006 which tackles all this mobile stuff quite well.

  3. 3 Joseph Rose Jul 26th, 2006 at 11:36 am

    So you guys have two installs of Django (one for each domain), or do you know which settings file to load based upon the url?

  4. 4 Jeremy Dunck Jul 26th, 2006 at 11:45 am

    (I don’t work for World, but do work with django in an environment doing the same thing.)

    It’s actually just one Django installation, but two different settings files.

    In apache’s vhost configuration, for the main site you do:
    SetEnv DJANGO_SETTINGS_MODULE main_site.settings

    and in the vhost for the mobile site, you do:
    SetEnv DJANGO_SETTINGS_MODULE mobile_site.settings

    Tada!

  5. 5 Joseph Rose Jul 26th, 2006 at 12:18 pm

    Thanks Jeremy!

  6. 6 Matt Croydon Jul 26th, 2006 at 12:47 pm

    Hey thanks for the clarification, Jeremy. Sometimes it’s dangerous to push the “post” button before caffeine.

  1. 1 Jakki Degg Trackback on Jun 19th, 2007 at 12:38 pm

Leave a Reply