S
S
stayHARD2015-08-13 13:36:37
Django
stayHARD, 2015-08-13 13:36:37

csv export from django/subprocess database?

Good afternoon.
You need to do something when you click on the button, all information from the database is exported to csv and it is downloaded.
View:

def export_view(request):
  """
  Export a csv file with data from database.
  """
  subprocess.call('python to_csv.py', shell = True)
  f = open('output.csv', 'r')
  response = HttpResponse(f, content_type='application/force-download')
  response['Content-Disposition'] = 'attachment; filename=' + 'output.csv'
  return response

to_csv.py
import pandas as pd
import sqlite3
import os
import subprocess

subprocess.call("rm output.csv", shell = True)
con = sqlite3.connect("db.sqlite3")
df = pd.read_sql("SELECT * FROM app_contacts", con)
df.to_csv('output.csv', index=False, encoding='utf-8')

On the local computer without virtualenv everything works fine. I'm trying to transfer to a server with a virtual environment created and nothing happens there. Rather, the file is downloaded, but it is old. That is, the rm command did not work. The rights are handles from the console everything turns out.
How can you do better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Fateev, 2015-08-13
@svfat

Is to_csv.py exactly executed? I didn't really understand why subprocess.call() should be used at all? Why not shove this code into the view? And why not remove with os.remove()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question