问题描述:

I have a dynamically created table with data from a django model. This table is displaying additional information about each data_element.

In the last column there should either

  • be a button displayed for each row, which will run the script with additional keywords from that specific data_element, without reloading or freezing the page.
  • If the script is still running (can take hours) there should be progress icon displayed and
  • if the script has already finished, there should be a button displayed, redirecting to an results.html

How can I program that with django?

Currently I am executing a script manually, but for that I am redirecting to another template with the args to parse and when the script is executed (with call_command('my_script', *args) the page freezes until the script ends.

<form action="{% url 'calculate' element_id %}">

<input class="btn btn-primary-custom" id="submit" type="submit" value="run script">

</form>

网友答案:

You will need a way to launch async tasks, and a way to get the task status, and then add quite some js/ajax code to launch the task and update your table accordingly.

For the first two parts, the canonical solution is celery.

网友答案:

Another, less recommended way to do that (less recommended, but probably easier to setup than celery) is to simply spawn a child, detached process which will complete the job in the background and store the status and results somewhere (e.g. in your database or in a memory storage like redis).

The main disadventage is that you lose control of what is happening in the child process. Depending on your project - if you don't care about the process control - it may turn out to be better solution.

相关阅读:
Top