Mobile App on Subdomain with Django
I’ve noticed a fairly common pattern arising with mobile and iPhone versions of websites using sub-domains. Clicky is an excellent example of this. They provide an iPhone version of their site at i.getclicky.com and a generic mobile version at m.getclicky.com.
I want to create something similar for Inzolo.com. I assumed that Django, with all its awesomeness could handle this without much fuss. But I really wasn’t sure how to put all the pieces together. Essentially, I need to create new sites on sub-domains that use the same models with different views and templates. My initial search of Google returned various results of simply creating mobile versions of existing websites by providing alternate templates, but that is not what I need to do (well, I suppose I ought to, but that’s not on the priority list yet). I don’t want my sales & info pages at the root of the site, I want the actual app that users log in to budget with – only a dumbed down version without all the ajax.
Creating a new project is one solution, but it is not ideal. I could have my settings point to the same database and copy or symlink my model, but it seems a bit kludgy.
So I went to where I normally go to get quick tips on Django – the #django IRC channel. jumpa was particularly helpful in providing the tip I needed.
Since I am using mod_wsgi, I can create an alternate settings file for my mobile sub-domains. That works great on my live site, but for my testing environment I don’t use mod_wsgi. I simply use “manage.py runserver”. I learned that is not much of an issue either as I can call “manage.py runserver –settings=mobile_settings 0.0.0.0:8001″
So now, I have the preliminary info I need, I can start building. Just one last thing. I figure I can use most of the existing settings in my main settings file, so I plan to just extend it to mobile_settings.py. At the top of mobile_settings.py I would just add the following, then proceed to overwrite/add the necessary settings:
from settings import *
** UPDATE **
As I suspected there wasn’t much to it. I created a new app in my project name mobile. I also created a new template directory for my mobile templates. Here is an example of what my mobile_settings.py file looks like:
from settings import * ROOT_URLCONF = 'mysite.mobile_urls' TEMPLATE_DIRS += ( "/path/to/my/mobile/templates", ) INSTALLED_APPS += ( 'mysite.mobile', )
Related posts:
- Basic Authentication on mod_wsgi I’m currently in the process of creating an iPhone app...
Related posts brought to you by Yet Another Related Posts Plugin.
March 3, 2010
Tags: django, iphone, mobile, python Posted in: Programming & Internet




9 Responses
I’d be interested in what the mobile version of settings.py ends up looking like – please post it when you’re done!
@Julian: I’ve posted my mobile_settings.py file for you.
You can just serve your Django project right at the root domain, and have a Middleware check the subdomain the client used. If it’s www or whatever, you keep going. If it’s, let’s say, “mobile”, you can have your apps use a different set of templates, or redirect request to a different set of views intended for mobile version.
On chattarati.com I use a context processor to detect if the request is on the m. subdomain or not then set a base_template variable accordingly. All my templates {% extends base_template %}. In addition, it sets an is_mobile variable that I use inside the templates for any conditional differences I want to make (for instance we serve different css and resize images to a smaller dimension).
When a mobile visitor hits the site they are presented with a link at the top of the page to view this page on a mobile optimized version. If they click that, once at m. they are presented with another link letting them set a preference (via cookie) to always view the site in the mobile version. This way if they click a link on Twitter, etc. they get automatically redirected by a middleware class.
Not knowing your particular app, I don’t know why you want to present a different URL structure, but I would say to make sure that if a user manually types in a URL that it’s properly redirected. About two thirds of the links I click on Twitter on my phone take me to a site that redirects me to a mobile version but not to the article I was trying to get to because the URL structures don’t match. Finally, the UX guy in me has to mention to give your users a link to switch out of the mobile version. It should be their option to choose.
@Henrique: I didn’t want to mess with a middleware solution (I actually wrote sub-domain middleware that is posted in djangosnippets) mainly because I didn’t want to mess with setting up hosts in my test environment. I prefer to call the run server command for testing because on any given day I could be working on my app on a Ubuntu box, my Macbook Pro, or my Windows 7 box. I’m just too lazy to set up such a development environment in each.
@dryan: There are various options documented on the Internet on how to serve up the same views but modified for a mobile platform, but as I mentioned in the post, my mobile app is a different (dumbed down) app altogether. I didn’t want to use any of my views or templates from the original site, although this solution leaves to door open to do so.
Having an active IRC channel is always a plus, as it really can make a lot of difference to get some real time help.
BTW, I like the dumbed down concept and not using the other templates. It seems like that would cut some overhead and end up with a lot better looking mobile version. I don’t use Django on any of my projects, but I have a few that need a mobile version…
BTW, I like the dumbed down concept and not using the other templates. It seems like that would cut some overhead and end up with a lot better looking mobile version. I don’t use Django on any of my projects, but I have a few that need a mobile version…
Hi, my question could be very simple but I didn’t found answer.
“Since I am using mod_wsgi, I can create an alternate settings file for my mobile sub-domains”
can someone explain how to do it?
I just migrated my website to webfaction, the desktop part work quite well while the mobile one give me some troubles.
Thanks
Wow these nerdy techy things are way above my head, if i have a problem i call in a man that can
Might not be as exciting but i don’t mess things up.
Leave a Reply