views:

323

answers:

6

I am trying to find the optimal architecture for an ajax-heavy Django application I'm currently building. I'd like to keep a consistent way of doing forms, validation, fetching data, JSON message format but find it exceedingly hard to find a solution that can be used consistently.

Can someone point me in the right direction or share their view on best practice?

A: 

I can't think of any standard way to insert ajax into a Django application, but you can have a look to this tutorial.

You will also find more details on django's page about Ajax

mnml
A: 

Two weeks ago I made a write up how I implement sub-templates to use them in "normal" and "ajax" request (for Django it is the same). Maybe it is helpful for you.

vikingosegundo
A: 

I make everything as normal views which display normally in the browser. That includes all the replies to AJAX requests (sub pages).

When I want to make bits of the site more dynamic I then use jQuery to do the AJAX, or in this case AJAH and just load the contents of one of the divs in the sub page into the requesting page.

This technique works really well - it is very easy to debug the sub pages as they are just normal pages, and jQuery makes your life very easy using these as part of an AJA[XH]ed page.

Nick Craig-Wood
Ok, sounds reasonable. But do you make separate view functions for AJAX requests and ordinary requests or do you use if_ajax() to return different responses? What about forms, do you use ordinary requests to submit form data or do you do it using AJAX?
Jonas Klemming
I make a view which works in the browswer first, then use jQuery to snatch the relevant bits out of it and insert it into the requesting page. I haven't used `is_ajax` yet. As for forms, I do use the jQuery form submission for dynamic pages yes. These POST to a normal django view.
Nick Craig-Wood
I'm an ajax/django newbie, but isn't your method very inefficient? It means returning a lot more than you actually need (all the surrounding HTML, instead of the actual data). Or am I missing something?
Edan Maor
It does return more data than is strictly necessary. However I think you would be hard put to measure the speed difference between that and contructing json/xml and returning that instead. It is certainly a lot easier to write and debug. If your programmer time is in scarcer supply that your bandwidth then AJAH wins over AJAX every time!
Nick Craig-Wood
A: 

+1 to Nick for pages displaying normally in the browser. That seems to be the best starting point.

The problem with the simplest AJAX approaches, such as Nick and vikingosegundo propose, is that you'll have to rely on the innerHTML property in your Javascript. This is the only way to dump the new HTML sent in the JSON. Some would consider this a Bad Thing.

Unfortunately I'm not aware of a standard way to replicate the display of forms using Javascript that matches the Django rendering. My approach (that I'm still working on) is to subclass the Django Form class so it outputs bits of Javascript along with the HTML from as_p() etc. These then replicate the form my manipulating the DOM.

Aaron Lockey