Django - view actions

I have an admin-driven function (importing a database) that may take some time to complete, so I want to show some feedback to the user during this time - for example, a progress bar or just some messages. Even sending the page in parts during lengthy activities would be sufficient.

What would be the easiest way to do this in Django?

+6
django progress-bar
source share
2 answers
  • Ajax Polling - using a client-side timer, you constantly check the server about this status. The process is as follows: the user sets up database data and downloads hits. The file transfer and page request runs an asynchronous process on the server to import the database. When a user clicks on a download, he starts a client timer, which sends AJAX request to the server at regular intervals to ask about it. The server returns JSON, and the client side of the script determines what it wants to do with it.

  • COMET - I am not so familiar with this, but traditional AJAX works as a client sending a request to the server. It is known as pull communication. In COMET, it presses. The server pushes data to the client about its progress, even if the server did not request it. This creates a situation with less server load than polling. Google shows some results for people using COMET with Django .

  • Reverse AJAX - similar to COMET. Reverse Ajax with Django .

(I'm sorry, I know the least about the last two, but I decided that you would at least want to know that they exist)

+3
source share

There is no way to do this without any client-side scripting, i.e. Ajax. You need something that will poll the server at regular intervals and show the answer to the user. There is a fragment that shows how this can be done.

Of course, to make this possible, you will also have to save the import itself on an autonomous process. This will do the import and record its progress somewhere regularly (in a file or in a database) so that Ajax can request it. A good way to do this might be to use celery , a Django-based distributed task queue.

Finally, you will need a simple view that will call Ajax, which will request a lengthy process (or view the created progress record) and report to the client.

So, pretty hard.

+1
source share

All Articles