1

I am new to Django and have completed the 7 part tutorial and am now trying to learn more by making my own app.

Suppose you are making an interactive data visualization application where the front end is powered by d3 (or your favorite JS library) and your data is coming from the server. Since your data is large, you first have to load it into server memory (maybe from a binary file, or however you store it). Yet you don't want your user to be looking at a blank page when they could be seeing the rest of the site (maybe filling in some parameters for the interactive data).

How can you, when a user requests a web-page, load and maintain data in memory on the server while Django still renders the page as well as maybe sends POST requests to update some server side information?

SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • Dynamic web pages typically use AJAX. https://www.w3schools.com/xml/ajax_intro.asp. I think Django has something called Django Channels which may also let you do something like that. Just some useful information: In your HTTPResponse you can change the "Content-Disposition" to get different data from the server. Normally the HTTPResponse will change the web page with HTML. The "Content-Disposition" allows APIs to send JSON data instead of a web page and allows you to download a file without changing your web page. – justengel Jun 05 '17 at 17:10
  • I think the other part of your question is about having the server send large data. This can be done with StreamingHttpResponse which uses a generator that iterates through the data sending it down to the client while still being able to process other requests. – justengel Jun 05 '17 at 17:15
  • @HashSplat can you make a mock example? – SumNeuron Jun 05 '17 at 18:07
  • Look here for a good example. https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications and here https://impythonist.wordpress.com/2015/06/16/django-with-ajax-a-modern-client-server-communication-practise/ – justengel Jun 05 '17 at 18:39

1 Answers1

4

Django doesn't have any asynchronous method to deal with what you're asking. The platform is request-based so there's no way to send the information later using the same request.

The solution in this case is using two views (or a view that can handle multiple formats, like @SumNeuron mentioned):

  1. The first view loads the page where you'll be loading the data later. On that page, an XMLHttpRequest is done to request the data.

  2. This view only sends the data, it can be just the data in JSON or it can be partial HTML made from a template.

fixmycode
  • 8,220
  • 2
  • 28
  • 43