How do I create a loading page when I render something in the backend using Django?

I have a master view function for my application. Upon successful login, this main view method is called and is expected to render the template.

But I have to do some calculations in this view method [I check certain conditions for the user by making a facebook graph api request.]

So it takes 2-4 seconds to download.

How to show this loading scene, since the template is rendered by a return statement and thus only executed after the process has finished.

Should I make 2 views, one to show the download and the other to calculate and keep making a request to AJAX

another viewer to check if the process is complete or not?

+3


source to share


2 answers


You really have to do two kinds: one to return a page displaying the loading UI and one to complete a long running task.

The second view will be called using an AJAX request made on the download page. The response from the AJAX request will notify your "download" page that it's time to move on.



You need to make sure that the duration of the AJAX request will not exceed your server timeout (with ~ 10 seconds, it should be fine).

+2


source


You need to run your Graph API requests on a task that is running asynchronously, which will allow you to return HttpResponse

without waiting for the task to complete.

Celery lets you do just that.

Then you need a way to notify the client when the asynchronous task is complete. I see two ways to do this:



  • Making AJAX requests at regular intervals to check if the task has completed.
  • Using WebSockets .

The first approach is the simplest, but it has the drawback of making a lot of useless queries and being less reactive.

Using WebSockets on the other hand will require more customization as an external application is required (ex: django-socketio or swampdragon ). If this is the only place where you need server-to-client notifications, using WebSockets seems overkill.

0


source







All Articles