How to save file uploaded into folder located out of Django project folder ?

Hi

I would like to save the uploaded file through models but save the physical file out of Django project folder such as shared-folder (\127.0.0.1\incident_docs).

How to implement this scenario?

Django project folder = C:\Django-WebApp\SMartApp
Shared-Folder = D:\incident_docs (shared path)

I have studied as the following links.
https://docs.djangoproject.com/en/3.1/ref/models/fields/#filefield

https://docs.djangoproject.com/en/3.1/topics/files/

I try as code below, it occurred error.

Message=The joined path (\127.0.0.1\incident_docs\incident_29\search-poor.png) is located outside of the base path component (C:\Django-WebApp\SMartApp)

  • Source=C:\Django-WebApp\SMartApp\app\decorators.py*
  • StackTrace:*
  • File “C:\Django-WebApp\SMartApp\app\decorators.py”, line 22, in wrapper_func*
  • return view_func(request, *args, *kwargs)
private_storage = FileSystemStorage(location=r'\\127.0.0.1\incident_docs')
def incident_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/incident_<id>/<filename>
    incidentID=instance.incident_ref.id
    incident_filename= filename
 
    x_path = '{0}\{1}{2}\{3}'.format(settings.PRIVATE_STORAGE_ROOT,settings.INCIDENT_PREFIX_DOC, incidentID, incident_filename)
    return (x_path)

class Incident_File(models.Model):

    incident_file = models.FileField('Upload File',upload_to=incident_directory_path, blank=True, max_length=254)

    incident_ref = models.ForeignKey(Incident, on_delete=models.CASCADE, related_name='files',blank=True)

Thank you.
Pongthorn

Review the FileField.upload_to docs.
Briefly, the path must be specified Unix-style, and would not include the host.
Also, you want to make sure your MEDIA_ROOT setting is correct, and that the path you’re providing is either based from root or relative to MEDIA_ROOT.

you want to make sure your MEDIA_ROOT setting is correct, and that the path you’re providing is either based from root or relative to MEDIA_ROOT.

I already understood.
Thank you.

Hi, here’s my code but it doesn’t work:

def path_and_rename(instance, filename):
ext = filename.split(‘.’)[-1]
# get filename
if instance.pk:
filename = ‘{}.{}’.format(instance.pk, ext)
else:
# set filename as random string
filename = ‘{}.{}’.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(‘account/’, filename)

class Profile(models.Model):
picture = models.FileField(upload_to=path_and_rename)

MEDIA_ROOT = “/Users/myuser/Desktop/django-sample/”
MEDIA_URL = “account/”

Does someone please help me?
Thank you

If you’re having an issue, I suggest you open up a new topic so as to not cause confusion in this one. In that issue, also please include details about exactly what isn’t working. Are there any error messages being thrown? Are things being uploaded to the wrong directory? Provide all the specifics regarding the symptoms you’re seeing.

Also, when posting code, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python.

Hi Ken thank you so much for your feedback, I have open a new issue:

1 Like