Reputation: 903
I'm trying to get django to serve CSV data from a Model to generate a graph using d3.js. This example works when I have the same CSV data sitting on the server and I pass scatter a path to the CSV file.
I'm following the solution posted here Direct data input from Django for a D3 graph to serve the CSV data.
I am getting an error in the scatter function claiming that the data passed is null.
def serve_data(request):
model = get_object_or_404(FileData, pk = 1)
data = model.get_data()
return HttpResponse(data, content_type = "text/csv")
<script type="text/javascript">
$.get('/site/serve_data/', function(data)
{
$('.result').html(data);
scatter(data); // Here's the call to the javascript function
});
</script>
function scatter(csvData)
{
// ERROR HERE: csvData is null
d3.csv(csvData, function(data)
{
data.forEach(function(d)
{
// work with data
});
});
});
Am I on the right track?
The serve_data
function does return CSV data when I call the URL from the browser.
Upvotes: 1
Views: 2587
Reputation: 32182
You are simply misusing d3.csv
, it expects a URL. See https://github.com/mbostock/d3/wiki/CSV#wiki-csv
Upvotes: 1