Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 feedback 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?

like image 298
hmp Avatar asked Oct 10 '09 19:10

hmp


People also ask

How do actions work in Django?

When a user call the action (the view to execute a certain task), Django immediately response back to the user and it's done. The job which has to be done was sent to the message broker where it will be actually executed.

How does Django know when a task is done?

When a user call the action (the view to execute a certain task), Django immediately response back to the user and it's done. The job which has to be done was sent to the message broker where it will be actually executed. We all know all of the things mentioned above. Question is "How does the user know when the task will finish?"

How do I change an object in Django admin?

The basic workflow of Django’s admin is, in a nutshell, “select an object, then change it.” This works well for a majority of use cases. However, if you need to make the same change to many objects at once, this workflow can be quite tedious.

How to display messages after form submit in Django?

Display messages after form submit in Django is not difficult as it seems to be. Here are the simple steps to implement message framework for your Django website. 1. Initialize the Messages App in Django Settings Add django.contrib.messages app entry in Django INSTALLED_APPS list in settings.py.


2 Answers

  1. 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.

  2. 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.

  3. 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)

like image 76
T. Stone Avatar answered Oct 05 '22 14:10

T. Stone


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.

like image 43
Daniel Roseman Avatar answered Oct 05 '22 12:10

Daniel Roseman