1

I am developing a website using django. The website has a page which takes more than 1 seconds to be rendered. I want to load this page progressively like Google. Google first loads the input then after some moments loads search results. like this:

enter image description here

I use StreamingHttpResponse (described here) to load page progressively. My views.py is here:

import time
from django.http.response import StreamingHttpResponse

class TestStreamingLoading(View):
    def get(self, request):
        response = StreamingHttpResponse(streaming_content=self.generate_stream_response())
        return response

    def generate_stream_response(self):
        yield 'data1 '
        time.sleep(4)
        yield 'data2'

My problem is the browser does not render anything till total page is downloaded. The browser shows nothing before 4 seconds and after that shows data1 data2. But I want to show data1 at the beginning and show data1 data2 after 4 seconds. How can I do this?

hamid
  • 694
  • 1
  • 8
  • 20
  • Use js on the front end. – bruno desthuilliers Dec 11 '19 at 12:55
  • @brunodesthuilliers you mean `ajax`? It is slow. – hamid Dec 11 '19 at 12:56
  • ajax is neither "slow" nor "fast" by itself, it's just a plain simple http request / response cycle. It will be "slow" if your server takes ages to return a response and / or if your js code (updating the page from the response) takes ages to do it's job. And anyway, this is basically how you implement progressive loading of a page - unless you want to go [the full PWA route](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Introduction) of course. – bruno desthuilliers Dec 11 '19 at 13:06
  • I do not want to send multiple `http` request to my server (one for loading part1 and another one for loading part2). It is slower than waiting for part2. I have tested `ajax` before. – hamid Dec 11 '19 at 13:10
  • Once again, whether it's "slower" or not depends on your server, your js code etc - the fact you "have tested ajax before" doesn't mean much here (I can easily write a pure C app that's slower than a python one). But well, let's wait and see if anyone comes with a better solution... – bruno desthuilliers Dec 11 '19 at 13:24
  • Oh and yes: if you haven't done so already (you didn't mention it) you may also want to try and fix the root issue (why is your page taking so much time to render) before thinking of progressive rendering. Also note that django has a rather effective cache system that can save a lot of time if well used. – bruno desthuilliers Dec 11 '19 at 13:27
  • Here is some similar questions: https://stackoverflow.com/questions/46705905/chrome-does-not-stream-content-from-djangos-streaminghttpresponse https://stackoverflow.com/questions/15116881/django-streaminghttpresponse-into-a-template But none of them did not work. – hamid Dec 11 '19 at 13:28
  • Your code seems okay. Maybe send data using appropriate content type. `text/plain` in your case. – xyres Dec 11 '19 at 14:29
  • @xyres This is `text/html` but does not work. – hamid Dec 11 '19 at 14:49

1 Answers1

1

The reason can be gzip middleware. gzip encoding makes the content to some chunks, so it waits to complete a chunk then will compress and send the chunk. If you want to load a streaming page, you should use brotli encoding middleware for django. brotli has been made for loading streaming page by Google. more information is here.

mohammad
  • 434
  • 1
  • 5
  • 17