I am storing the CSV file in the static folder. This is working perfectly when I set Debug to True.
d3.csv("{% static 'app/data/data.csv' %}" ...
However, as soon as I set Debug to False (for production), I get a 404 error saying that the file does not exist. I am trying to get around this by serving the CSV file on a separate page and using
d3.csv("{% url 'app:data' %}" ...
Here's what the function to serve the CSV file in views.py looks like:
def data(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'inline; filename="data.csv"'
with open("...absolute path.../data/data.csv", "rb") as csvData:
csv_list = list(csv.reader(csvData))
csvData.close()
csv_data = csv_list
t = loader.get_template('app/data.txt')
c = Context({'data': csv_data})
response.write(t.render(c))
return response
and data.txt:
{% for row in data %}
{{row.0}},{{row.1}}
{% endfor %}
The problem with this method is that I get a page that contains "data" file (no .csv) in the resources and there is an extra new line after every row.
Any recommendation on what I can do or is there a better way to deal with this?