"file wasn't available on site" error while it does exist, Django

1.5k Views Asked by At

I am programming a web that gets a .csv or .xlsx dataset, processes it, and returns it to the user using Django. I should say it is working OK on localhost. but not in a real server.

views.py for creating a project and receiving the file:

from django.core.files.storage import FileSystemStorage


def create_prject(request):
    if request.method == 'POST':
        user = request.user
        name = request.POST.get('pname')
        data = request.FILES['data']
        frequency = request.POST.get('frequency')
        unit = request.POST.get('unit')

        fs = FileSystemStorage(location=f'Media/Projects/{user}')
        filename = fs.save(data.name, data)
        dataurl = f'/Media/Projects/{user}/{filename}'
        header = request.POST.get('header')

        if header is not None:
            Project.objects.create(ProUser=user, Name=name, Data=filename, DataURL=dataurl,
                                   Header=True, Frequency=str(frequency), Unit=unit)
        else:
            Project.objects.create(ProUser=user, Name=name, Data=filename, DataURL=dataurl,
                                   Header=False, Frequency=str(frequency), Unit=unit)

    return redirect('dashboard')

and after processing I want to return to user the files (the raw file and processed file must be downloadable).

views.py after processing:

def process(request):
    user = request.user
    if not user.is_authenticated:
        return redirect('login')
    else:
        if request.method == 'POST':

            # Some Processing Functions and creating the pandas.DataFrame

            excel_file = f'Despiked - {project.Name}.xlsx'
            dff.to_excel(excel_file, index=False)
            fs = FileSystemStorage(location=f'Media/Projects/Despiked/{user}')
            filename = fs.save(excel_file, open(excel_file, 'rb'))
            os.remove(excel_file)
            project.Processed_Data = filename
            project.Processed_DataURL = f'/Media/Projects/Despiked/{user}/{filename}'
            project.save()
            return redirect('dashboard')

the download button will be shown in the template. but when I click on each file (raw or processed) I get the "File wasn't available on site" error. while it does exist in the file manager from CPanel. For example, the URL in CPanel is /Media/Projects/Despiked/farzad/Despiked - Test File.xlsx and the download link href is the same. what am I missing?
enter image description here
Any help would be greatly appreciated.

0

There are 0 best solutions below