[Answer]-Django: Can I do a div ajax refresh in template on object votes and IDs without going to Views?

1đź‘Ť

I don’t know why you say Django isn’t good for asynchronous requests. It’s perfectly fine for those: they are not different in any substantive way from normal ones. For your very simple task, there is no need though for any of this, or any of the tools you mention: you don’t want anything asynchronous in the first place.

There are several problems here. Firstly, most of your attempts appear to be rendering the entire voting.html template. That’s not what you want, is it: you just want to render a portion, the bit that contains the vote that you want. In addition, you’re passing a single vote instance, v, as the “voting_entry_list” context variable, which means that when the template tries to iterate through the list in the for loop, it will raise an exception because a single instance is not iterable. You’d probably find, if you inspected the Ajax response with the browser developer tools, that most of your attempts would have shown that “not iterable” exception.

Then, there are the problems with your Javascript itself. I have absolutely no idea why you have put location.reload() in both your handlers. That causes the entire page to be refreshed, making a whole new – and non-Ajax – request to the server, completely ignoring the Ajax response you already had, and refreshing the whole page. Instead of that, you need to insert the HTML you get in that Ajax response into the appropriate place in the page, via the normal jQuery DOM manipulation functions.

Leave a comment