Whip up an AJAX API for your site in a jiffy:
class MySite(AJAXApi):
@AJAXApi.export
def hello(request):
return {'content': self.get_content()}
def get_content(self):
return 'Hello, world!'
urlpatterns += MySite().url_patterns()
(the example needs the JSON encoding middleware of [snippet 803](http://www.djangosnippets.org/snippets/803/) to work.)
The secret is that bound instance methods are callable too, so work as views. (Most Django people only use functions, or sometimes classes with `__call__`, as view functions.)
You get implicit type dispatch off that `self` object. So you could subclass `MySite`, change `get_content`, and still use the same `hello` method.
See (django-webapp)[http://code.google.com/p/django-webapp/] for a REST-ish Resource class using this same idea.
You can clearly do better than my `func_to_view`, and also make a better decorator than `exported` (so you can actually say `@exported('name') def function()` etc.). This is more of a proof of concept that should work for most people.
Caveat: I've changed a few things since I last really tested this.
(psst, this also works for non-AJAX views too.)