djangosubclassajax-uploaddjango-file-upload

changing UPLOAD_DIR for django-ajax-uploader


Newbie here sir. I manage to install django-ajax-upload to my django study project. And using it's initial view, url and template to see how it works. I successfully uploaded a file to it's default directory 'upload'.

Now, I'm trying to change the UPLOAD_DIR used by django-ajax-upload' to something like 'endorsement' folder.

About django-ajax-upload: UPLOAD_DIR is located in local.py under class LocalUploadBackend.

/ajaxuploader/backends/local.py

class LocalUploadBackend(AbstractUploadBackend):
    UPLOAD_DIR = "uploads"

    def setup(self, filename, *args, **kwargs):
        self._path = os.path.join(
            settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
        try:
            os.makedirs(os.path.realpath(os.path.dirname(self._path)))
        except:
            pass
        self._dest = BufferedWriter(FileIO(self._path, "w"))

The ajax action is calling the import_uploader = AjaxFileUploader() to upload the file. Where AjaxFileUploader has this __init__

class AjaxFileUploader(object):
    def __init__(self, backend=None, **kwargs):
        if backend is None:
            backend = LocalUploadBackend
        self.get_backend = lambda: backend(**kwargs)

I'm not sure if this the right way to change the UPLOAD_DIR thru subclass. Here's my code.

from ajaxuploader.views import AjaxFileUploader
from ajaxuploader.backends.local import LocalUploadBackend

class myajaxfileuploader(AjaxFileUploader):
    def __init__(self, backend=None, **kwargs):
        local = LocalUploadBackend.UPLOAD_DIR
        local = "endorsement"
        super(myajaxfileuploader,self).__init__(backend=local, **kwargs)

I can see the file upload file button but gives me a upload failed. What is the correct way of doing this?


Solution

  • *this the problem for not reading the django-ajax-upload github issue section, the solution was there all along

    I change my initial import_uploader = AjaxFileUploader() to import_uploader = AjaxFileUploader(UPLOAD_DIR='endorsement')

    django-ajax-upload support this arguments to change the upload_dir ..grr...