Opening a data file from a media directory in Django

I have an application that allows users to upload CSV files with data that are then displayed and displayed to the user. These files are saved as media in my media folder. However, in my graphical representation, I need to open the file and process it. My problem is that I can only open files that are in the current working directory of my project, and at any time when I try to download a file from some one from this directory, I get this error:

File b'TEST.CSV' does not exist

I tried to do this, but to no avail:

file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'Data_Files')
data_file = open(os.path.join(file_upload_dir, new_file), 'rb')

A variable new_fileis just the name of the file stored in the session, not the path to this file. Data_Filesis the directory in the media directory that contains the downloaded files.

My multimedia settings for Django:

SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')

Is there a way to correctly reference media files from a view?

Here is the result file_upload_dirand location of the new file.

>>> print(file_upload_dir)
C:\\Users\\vut46744\\Desktop\\graphite_project\\media\\Data_Files

>>> print(os.path.join(file_upload_dir, new_file))
C:\\Users\\vut46744\\Desktop\\graphite_project\\media\\Data_Files\\TEST.CSV
+4
source share
1 answer

Normally you should not access files using open()in a Django application. You must use the storage API. This allows your code to play well with Django settings and potential third-party applications that complement this API. https://docs.djangoproject.com/en/1.7/topics/files/#file-storage

So here you should do something like

from django.core.files.storage import default_storage

f = default_storage.open(os.path.join('Data_Files', new_file), 'r')
data = f.read()
f.close()
print(data)

, , , , , . , MEDIA_ROOT. settings.UPLOADS_ROOT ( - MEDIA_ROOT, ).

# Put this in a storage.py files in your app
from django.conf import settings
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.functional import LazyObject

class UploadsStorage(FileSystemStorage):
    def __init__(self, location=None, base_url=None, *args, **kwargs):
        if location is None:
            location = getattr(settings, 'UPLOADS_ROOT', None)
        super(UploadsStorage, self).__init__(location, base_url, *args, **kwargs)
        self.base_url = None  # forbid any URL generation for uploads

class ConfiguredStorage(LazyObject):
    def _setup(self):
        storage = getattr(settings, 'UPLOADS_STORAGE', None)
        klass = UploadsStorage if storage is None else get_storage_class(storage)
        self._wrapped = klass()
uploads_storage = ConfiguredStorage()

. , . , .

, :

from myapp.storage import uploads_storage
f = uploads_storage.open(new_files, 'r')

UPLOADS_ROOT . , - -. - , UPLOADS_STORAGE , .

+5

Source: https://habr.com/ru/post/1568387/


All Articles