Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the "best practices" for AJAX with Django (or any web framework)

Tags:

ajax

django

I am developing an issue tracking application in Django, mostly for a learning exercise but also for my own projects - and I am looking into using some AJAX for "enhanced" usability. For example, allowing users to "star" particular issues, which would add them to their watch list. This is implemented in a lot of sites, and is often AJAX - as the URL that the user is viewing doesn't need to change when they click the star.

Now, I am wondering what kind of response to return from my star_unstar view - that detects whether the request is being made via AJAX or not.

At present, if the request is an AJAX request, it returns just the section of HTML that is needed for the star, so I can replace the HTML in the star's parent DIV, so as the star appears "on" or "off", depending on the user's action.

However, I would much rather return some kind of JSON object, as it just seems more "proper", I think. The problem with this method is that the javascript would have to modify the star image's src attribute, the href on it, and the link title also, which seems a lot of work for such a simple feature. I am also looking into in-line commenting in the future, but I want to get a feel for how things "should" be done before I start coding lots of JS.

What is the general consensus when implementing features such as this, not just with Django, but all frameworks that operate in a similar way?

like image 891
Rob Golding Avatar asked Sep 22 '09 21:09

Rob Golding


2 Answers

When I work with Ajax my main concern is usually to limit the amount of data I have to send. Ajax applications of this type should be very responsive (invisible if possible).

In the case of toggling a star, I would create the actual on/off states as CSS classes, StarOn and StarOff. The client will download both the off and on star when they first visit the page, which is acceptable considering that the star is a small image. When you want to change the star appearance in the future, you'll only be editing CSS, and won't have to touch the javascript at all.

As for the Ajax, I'd send back and forth one thing -- a JSON variable true/false that says whether or not the request was successful. As soon as the user clicks on the star, I'd change it to the StarOn state and send out the request. 99% of the time Ajax will return true and the user will not even realize that there was some sort of delay in the web request. In the rare case where you get a false back, you'll have to revert the star to StarOff and display an error message to the user.

like image 80
Kai Avatar answered Nov 09 '22 12:11

Kai


I don't think your question relates particularly to Django or Python, as you point out at the end.

There's a lot of personal preference in whether you return a blob of HTML to write into the DOM or some serialized data as JSON. There are some practical factors you might want to take into account though.

Advantages of HTML: - Easy and fast to write straight into the page.

Advantages of JSON: - Not coupled to the front-end of your application. If you need that functionality anywhere else in the application, it is there ready to go.

My call on it. It's only a relatively trivial amount of HTML to update, and I'd probably go for returning JSON in this case and giving myself the extra flexibility that might be useful down the road.

like image 41
Andy Hume Avatar answered Nov 09 '22 11:11

Andy Hume