将Django StreamingHttpResponse转换成模板 [英] Django StreamingHttpResponse into a Template

查看:259
本文介绍了将Django StreamingHttpResponse转换成模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚出来的Django 1.5发布了StreamingHttpResponse。
现在,我阅读了这个讨论和第二个答案实际上打印出一个页面中的流(实际上只是数据)。



我想做的是将流响应的输出打印到模板中,不仅仅打印出讨论中的数据。



我该怎么办?我必须使用javascript并调用实现StreamingHttpResponse的视图,或者有一种方法来告诉django渲染模板,然后将StreamingHttpResponse数据发送到模板(然后我需要知道数据存储的变量是什么) ?



编辑:到目前为止我发现的解决方案是将最后的html页面的片段写入生成器(yield)。这个解决方案的问题是,我不能像例如一个随着数据流传播的一个吧(像一个加载栏)。



ciao

解决方案

是的,但可能不是你真的想要,因为整个模板将被迭代。然而,对于什么是值得的,您可以为模板传送一个重新渲染的上下文。

  from django.http import StreamingHttpResponse 
从django.template导入上下文,模板

#模板代码在创建时解析一次,所以
#在模块范围内创建更好的性能

t =模板('{{mydata}}< br /> \\\
')

def gen_rendered():
为范围(1,11)中的x:
c =上下文({'mydata':x})
yield t.render(c)

def stream_view(request):
response = StreamingHttpResponse(gen_rendered())
返回响应

编辑
您还可以呈现模板,只是追加< p> < tr> 标签,但与目的相反的模板。 (即将代码分开)

  from django.template import loader,Context 
from django.http import StreamingHttpResponse

t = loader.get_template('admin / base_site.html')#或任何
buffer =''* 1024

def gen_rendered():
yield t.render(Context({'varname':'some value','buffer':buffer}))
#^^^^^
#embed {{buffer}}你的模板
#(除非已经足够长了)强制显示

对于范围(1,11)中的x:
yield'< p> x = {}< ; / p> {} \\\
'.format(x,buffer)


Django 1.5 is just came out and it ships the StreamingHttpResponse. Now, I've read this discussion and the second answer actually prints out the stream in a page (actually just data).

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

What should I do? do I've to use javascript and call the view that implements the StreamingHttpResponse, or there's a way to tell django to render the template and later send the StreamingHttpResponse data to the template (then I need to know what's the variables where data are stored)?

Edit: the solution I found so far is to write the pieces of the final html page into the generator (yield). The problem of this solution is that I can't have, for example, a bar that grows with the data streamed (like a loading bar).

ciao

解决方案

Yes, but it may not be what you really want, as the entire template will be iterated. However, for what it's worth, you can stream a re-rendered context for a 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 render a template and just append <p> or <tr> tags to it, but it's quite contrary to the purpose of templates in the first place. (i.e. separating presentation 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's already long enough) to force display

    for x in range(1,11):
        yield '<p>x = {}</p>{}\n'.format(x, buffer)

这篇关于将Django StreamingHttpResponse转换成模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆