Simple ‘on-server’ mobile AJAX in Django
–or–
“How your mobile site may not be much more complicated than building one with a data API or AJAX”
I am working on a project called ‘IvyRoot’ that is predominantly mobile and which is going to make me fabulously rich and famous.
OK, OK. It’s predominantly mobile, at least. It’s all in Django.
There’s a ‘desktop’ web site to go with it, and this contains panels within pages. Some of these panels might take a few moments to process and so I use XHR to call back to get them once the main page has loaded, displaying a pretty spinning icon in the meantime.
I also use XHR to populate in-page tables which might have many rows – so that pagination and sorting are fast without having to load the whole page again.
I also want to expose a number of data APIs (providing XML, JSON, basic HTML and the like) so that people can build 3rd party clients, their own consuming sites, and widgets.
Finally, I want to provide a mobile web version of the site. I’ll probably do some sort of device detection to refine the behaviour, but I should assume that plenty of browsers will not be able to do the XHR bit, and should have the page (containing all the panels) in one go – admittedly after a slightly longer wait.
Well, it occurs to me that these requirements can all be solved at the same time. Switching templates based on URL (or other HttpRequest clues) is pretty easy, so one thing I could do is have a filename extension convention for all my URLs. Imagine a ‘things’ view that lists things.
- http://ivyroot.com/things – this is the human-consumable page: the view surrounded by menus, logos, graphics and the like. Knows how to switch outer template, based on various things, between desktop and mobile.
- http://ivyroot.com/things.html – this is the table at the heart of the page, perhaps using unstyled markup containing the list of things itself.
- http://ivyroot.com/things.xml, http://ivyroot.com/things.json etc – data APIs that list things in various serializations.
So perhaps we can set up URL routes like this:
... (r'^things