1

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?

tushar
  • 741
  • 1
  • 8
  • 21
  • With debug turned off Django won't handle static files for you any more - your production web server (Apache or something) should take care of that. – Vaulstein Jan 11 '16 at 08:35
  • Possible duplicate of [Why does DEBUG=False setting make my django Static Files Access fail?](http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – Vaulstein Jan 11 '16 at 08:37
  • Sorry, see my answer below. I changed the formatting and it's working fine now. Appreciate the response! – tushar Jan 11 '16 at 08:43

1 Answers1

0

Never mind, seems like it was just wrong formatting apparently. Works fine if I change the data.txt to:

{% for row in data %}{{row.0}},{{row.1}}
{% endfor %}
tushar
  • 741
  • 1
  • 8
  • 21