pythondjangobackground-processbackground-tasklong-running-processes

How to update background task status periodically in Django?


I have a django project which contains a long running process. I have used django-background-tasks library for this. It works but I want to create a pending page for users and display the status of the task. I should refresh that page every 60 seconds and update the status. How can I do that?

Thank you.


Solution

  • Hope you know about Ajax.

    How to use Ajax with Django: https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

    How to run Ajax code every n seconds: https://thisinterestsme.com/ajax-request-every-10-seconds/

    If you want to load a page partially, then you should split the page into two parts. One should contain the particular div which you want to refresh or reload, consider the page name as partial_load.html. And other page can have remaining codes consider the file name as full_page.html. The partial_load.html can be included inside the full_page.html by using include tag ({% include "partial_load.html" %})

     def refresh_page(request):
          if request.is_ajax():
               now = timezone.now()
               pending, running = get_process_status()
               context = {
                 "pending": count_pending, 
                 "running": count_running
                 }
               return render(request, "partial_load.html", context)
    

    full_page.html

     <html>
      <div> Something </div>
       .....
       <div id="status">
        {% include "partial_load.html" %}
       </div>
      ......
      <div> some more stuff </div>
     </html>