0

I have a long process wherein my application communicates with a server, saves a model based on the server's response, and restarts its communication. I'd like to present the user with a loading page which updates whenever a model is saved. For example:

Working on request 1...
Saving model 1...

Working on request 2...
Saving model 2...

Finished task. 
[Back to Homepage]

I've read through this(jango - show loading message during long processing) and its links; the first suggestion would not work, and the second requires threading, caching, etc. I've also read through the documentation for Signals.

Let's say the model which is saved is ModelAfterResponse. My view might look like this:

@receiver(post_save, sender=ModelAfterResponse)
def loading_page(sender, instance, created, **kwargs):
    model_name = kwargs['instance'].name
    return render(request, 'name/javascript_template.html', {"name": name}, content_type="application/x-javascript")

And the javascript_template.html might look like this:

<script>
$('#some_loading_page_specific_div').innerHTML = "Working on " + {{ name }}
</script>

This is just quick mock-code—but do I have the gist of it? When I decorate a function as above, and do something simple like return HttpResponse(":D"), nothing happens—everything continues running and :D is nowhere to be found.

I believe I'm on the right track. Is there a simple way to accomplish what I'm looking for?

AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
  • No, unfortunately you are not on the right track. This is not what signals are for and you cannot render data from one. Anything that renders data needs to be in a view. – Daniel Roseman May 17 '18 at 14:01
  • @DanielRoseman Ah, bummer. If you don't mind my asking, what *are* signals used for, if not this? Also, is there a means of updating a page in the way I intend without having to reload the page/stop the process? I'm not sure how to call AJAX from within a view (or why I would want to if I'm already in the view!) – AmagicalFishy May 17 '18 at 14:04
  • Signals can be for doing things like updating the database or sending emails. I don't understand what you mean about calling Ajax from within a view though; you call Ajax from your HTML. – Daniel Roseman May 17 '18 at 14:20
  • @DanielRoseman Ah. What I meant was something like: In order to call AJAX when a model is saved (which updates a loading page), I would presumably need to trigger something HTML related from within the view (when the model is saved). If I use `return HttpResponse()`, this would stop the for loop. I'm not sure how I could go about updating the loading page w/o using signals, and w/o returning something from the view – AmagicalFishy May 17 '18 at 14:27

0 Answers0