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?
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.
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?"
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With