Reputation: 631
So very briefly, I have an html table implemented with a Django template that displays some information that I have stored in my database. I want this (html) table to stay current and to automatically update if and when the database changes. I have looked around, but haven't found much literature on the subject. Would the best way of doing this be making an Ajax call every so often to update the table? If so, what would a standard amount of time between calls be?
Thanks a lot, and let me know if there is any more information that is needed.
Upvotes: 3
Views: 1987
Reputation: 1930
Basically the solution you're proposing yourself is called long polling. Django can't do this out of the box, but there are several add-on solutions, i.e. here. It wouldn't be too hard to whip it up yourself with some clever $.ajax calls on the client side.
A more thorough solution would not be to use long polling directly, but to only use it as a fall back for other server-side initiated events, like WebSockets and Server-Sent Events (SSE). These are however are harder to implement, because the WSGI specs don't offer these. You will have to have either a webserver that handles this outside of WSGI (Tornado comes to mind) or have another webserver (probably node.js) or an event handler like gevent that handles the messages the server sends through WebSockets/SSE.
Socket.io provides a fall-back from WebSockets to long polling and you would need something like this to get things done on the Django side.
There's a rather good blog post about socket.io, django and gevent here.
Upvotes: 5
Reputation: 1
You should probably use django signals: https://docs.djangoproject.com/en/1.7/ref/signals/ So the template can be updated each time the db change. You can use for example post_save signal sent each time the save() method is called. A simpler approach is to refresh the template at a choosen rate though it's not optimized.
Upvotes: -1