Django StreamingHttpResponse in Template

Django 1.5 has just left and it is sending StreamingHttpResponse. Now I read this discussion , and the second answer actually outputs the stream on the page (actually just the data).

What I want to do is print the output of the stream response to the template, and not just print the data, as in the discussion .

What should I do? should I use javascript and call a view that implements StreamingHttpResponse, or is there a way to tell django about the rendering of the template and then send the StreamingHttpResponse data to the template (then I need to know which variables are stored in the data)?

Edit: The solution I have found so far is to write fragments of the last html page to a generator (output). The problem with this solution is that I cannot have, for example, a strip that grows with data streams (for example, the download bar).

ciao

0
django stream templates
source share
1 answer

Yes, but it may not be what you really want, as the whole template will be repeated. However, for what it's worth, you can pass a re-rendered context to the template.

from django.http import StreamingHttpResponse from django.template import Context, Template # Template code parsed once upon creation, so # create in module scope for better performance t = Template('{{ mydata }} <br />\n') def gen_rendered(): for x in range(1,11): c = Context({'mydata': x}) yield t.render(c) def stream_view(request): response = StreamingHttpResponse(gen_rendered()) return response 

EDIT : You can also display a template and simply add <p> or <tr> tags to it, but this completely contradicts the purpose of the templates in the first place. (i.e. separation of view from code)

 from django.template import loader, Context from django.http import StreamingHttpResponse t = loader.get_template('admin/base_site.html') # or whatever buffer = ' ' * 1024 def gen_rendered(): yield t.render(Context({'varname': 'some value', 'buffer': buffer})) # ^^^^^^ # embed that {{ buffer }} somewhere in your template # (unless it already long enough) to force display for x in range(1,11): yield '<p>x = {}</p>{}\n'.format(x, buffer) 
+2
source

All Articles