django - 查看函数的发生器

时间:2015-06-20 22:18:32

标签: javascript python django django-views

在django中,是否可以创建生成器而不是函数。

现在我在view.py

中有这个功能
def first_case(request):
    data = [[1,2],[4,7]]
    template = loader.get_template('usecases/index.html')
    context = RequestContext(request, {'data': data,})
    return HttpResponse(template.render(context))

是否可以创建一个向模板生成数据的生成器?

这样的事情:

def first_case(request):
    template = loader.get_template('usecases/index.html')
    for data in generator_data():
        context = RequestContext(request, {'data': data,})
        yield HttpResponse(template.render(context))

这是index.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>The d3 test</title>
<style>
.chart {

}

.main text {
    font: 10px sans-serif;  
}

.axis line, .axis path {
    shape-rendering: crispEdges;
    stroke: black;
    fill: none;
}

circle {
    fill: steelblue;
}

</style>
        <script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js"></script>
      </head>
      <body>
        <div class='content'>
          <!-- /the chart goes here -->
        </div>
         <!-- {% load static from staticfiles %} -->
        <!-- // <script type="text/javascript" src = "{% static "usecases/scatter.js" %}" ></script> -->
        <script type="text/javascript">

            var data = {{data}};

            var margin = {top: 20, right: 15, bottom: 60, left: 60},
                          width = 960 - margin.left - margin.right,
                          height = 500 - margin.top - margin.bottom;

            var x = d3.scale.linear()
                .domain([0, d3.max(data, function(d) { return d[0]; })])
                .range([ 0, width ]);

            var y = d3.scale.linear()
                .domain([0, d3.max(data, function(d) { return d[1]; })])
                .range([ height, 0 ]);

            var chart = d3.select('body')
                .append('svg:svg').attr('width', width + margin.right + margin.left)
                .attr('height', height + margin.top + margin.bottom)
                .attr('class', 'chart')

            var main = chart.append('g')
              .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
              .attr('width', width)
              .attr('height', height)
              .attr('class', 'main')   

            // draw the x axis
            var xAxis = d3.svg.axis()
              .scale(x)
              .orient('bottom');

            main.append('g')
              .attr('transform', 'translate(0,' + height + ')')
              .attr('class', 'main axis date')
              .call(xAxis);

            // draw the y axis
            var yAxis = d3.svg.axis()
              .scale(y)
              .orient('left');

            main.append('g')
              .attr('transform', 'translate(0,0)')
              .attr('class', 'main axis date')
              .call(yAxis);

            var g = main.append("svg:g"); 

            g.selectAll("scatter-dots")
              .data(data)
              .enter().append("svg:circle")
                      .attr("cx", function (d,i) { return x(d[0]); } )
                      .attr("cy", function (d) { return y(d[1]); } )
                      .attr("r", 8);
        </script>
      </body>
    </html>

2 个答案:

答案 0 :(得分:0)

我知道有StreamingHttpResponse

答案 1 :(得分:0)

我没有确切的答案,但也许以下内容对您有所帮助。根据Wolkodav的建议,你应该使用StreamingHttpResponse:

import time
from django.http import StreamingHttpResponse
from django.views.generic import TemplateView

class StreamView(TemplateView):
    template_name = "usecases/index.html"
    response_class = StreamingHttpResponse

    def stream_response_generator():
        for x in whatever:
            yield 'whatever_string'
            time.sleep(1)

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(self.stream_response_generator())

在urls.py中:

url(r'^your_url$', StreamView.as_view())