Direct data input from Django for a D3 graph

前端 未结 2 879
失恋的感觉
失恋的感觉 2021-01-05 08:59

It seems all the D3 example graphs take an external .csv or .tsv file as input data. Is there any way to modify the code to take data from a variable in Django. Suppose {{ d

2条回答
  •  长情又很酷
    2021-01-05 09:43

    You can always make a view which will serve dynamic csv file which will be consumed by D3. This way will also allow users to download the data in case they need the raw data instead of a graph.

    def foo(request, ...):
        model = get_object_or_404(Foo, ...)
        data = model.get_data() # should return csv formatted string
        return HttpResponse(data, content_type='text/csv')
    

提交回复
热议问题