Home > Mobile >  read csv file from user input in django
read csv file from user input in django

Time:11-03

ERROR :

FileNotFoundError: [Errno 2] No such file or directory: '/media/tableConvert.com_v02pvt.csv'

Hello there im trying to read a csv file in django backend where user upload and i dont want to save it to my DB so my best try was this :

View :

def view_submit_form(request):
    if request.method == 'POST':
        text = request.POST.get('smstext')
        csv_file = request.FILES['csv_file']
        file_name = default_storage.save(csv_file.name, csv_file)
        file_url = default_storage.url(file_name)
        df = pd.read_csv(r'{}'.format(file_url))

    return render(request, 'SubmitForm/form.html',)

CodePudding user response:

You can read it from the request itself without saving it. Using ".file" command you can fetch the readable state of the file.

def view_submit_form(request):
    if request.method == 'POST':
        text = request.POST.get('smstext')
        csv_file = request.FILES.get('csv_file')
        df = pd.read_csv(csv_file.file)
    return render(request, 'SubmitForm/form.html',)

Shell command as image

  • Related