Django - display action progress
I have an admin-controlled feature (importing database) that can take some time to finish, so I want to show some feedb开发者_如何学Goack to the user during that time - for example a progress bar, or just some messages. Even sending the page in parts during the long action would suffice.
What would be the simplest way to do it in Django?
Ajax Polling -- Using a client-side timer, you constantly poll the server about it's status. The process is like this: The user configures the database details and hits 'upload'. The file transfers and the page request starts an asynchronous process on the server to perform the database import. When the user clicks upload it starts a client-side timer which at regular intervals sends an AJAX request to the server to ask it about it's progress. The server returns JSON and the client side script figures out what it wants to do with it.
COMET -- I'm not as familiar with this, but traditional AJAX works by the client sending a request to the sever. It's known as 'pull' communication. In COMET, it's push. The server pushes back data to the client about it's progress, even if the server didn't ask for it. This creates a situation with less strain on the server than polling. Google turns up some results for people using COMET with Django.
Reverse AJAX -- Similar to COMET. Reverse Ajax with Django.
(I apologies, I know the least about the last 2, but I figured you'd at least like to know they exist)
There's no way to do this without some sort of client-side scripting, ie Ajax. You need something that will poll the server at regular intervals and show a response to the user. There's a snippet that shows how this might be done.
Of course, to make that possible you'll also have to farm off the import itself to an off-line process. This would do the import, and record its progress somewhere regularly (in a file, or the database) so that the Ajax can query it. A good way of doing this might be to use celery, the Django-based distributed task queue.
Finally, you'll need a simple view that the Ajax will call, which will query the long-running process (or look at the progress record that it creates) and report back to the client.
So, fairly complicated.
精彩评论