0

I have a Python Django application I'm working on. There is a submission page that takes some input from the user and I spend time working on it with a thread. This processing can take variable time, as low as one minute to maybe 30 minutes. While the thread is running, I redirect to a waiting page, but I want to redirect from that waiting page to some output page when the processing thread completes.

I can have the thread create all necessary changes i.e. creating the page template, appending some code to the views.py file, and adding the path to the urls.py file, but I'm not sure how to have that redirect trigger.

I believe the overall question here is how can I redirect to a page in Django only if it exists?

  • I would say the main question is: how the server will change user's page when he will be ready? To do this, I would suggest [django-channels](https://channels.readthedocs.io/en/latest/) as the best solution but there might be some other ways – Christophe Mar 04 '22 at 19:51

1 Answers1

0

One possibility is to poll the results page. When it doesn't return a 404, you load the page. This answer by KyleMit to another question should work in your situation. To adapt his code:

const poll = async function (fn, fnCondition, ms) {
  let result = await fn();
  while (fnCondition(result)) {
    await wait(ms);
    result = await fn();
  }
  return result;
};

const wait = function (ms = 1000) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
};

(async() => {
    const url = "/result.html";
    let fetchResult = () => fetch(url);
    let validate = result => !result.ok;
    let response = await poll(fetchResult, validate, 3000);
    if (response.ok)
        location.replace(url);
    })();

Disadvantages of this approach are:

  • Polling increases network traffic
  • You load the page twice.

The advantage is that you can make the results page a parameter, then the user can close the page then come back later and continue waiting.

mbakereth
  • 186
  • 6