How to upload file in django and access in another module?

55 Views Asked by At

I'm usind django e plotly dash to make a dashboard in my page. In this application we will to make a upload file, but this file have to save in "C:\tmp\Uploads". How can i make this and access with python dash???

I make the form and can make the upload file, but can't access in another module.

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def test_page(request):
    if request.method == 'POST':
        upload_file = request.FILES['document']
    return render(request, 'index.html')
1

There are 1 best solutions below

0
OH Yucheol On

Did you save the uploaded file in "C:\tmp\Uploads"? If not, save the uploaded file in "C:\tmp\Uploads" like below.

from django.core.files.storage import FileSystemStorage

...

def test_page(request):
    directory='C:\\tmp\\Uploads\\'
    if request.method == 'POST':
        upload_file = request.FILES['document']
        fs = FileSystemStorage(location=directory)
        filename = fs.save(upload_file.name, upload_file)

        return render(request, 'index.html')

    return render(request, 'index.html')

Then, read the uploaded(and saved) file in your ploty-dash module. It will works.

References

  1. How to Upload Files With Django

  2. Simply save file to folder in Django