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).
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)
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.
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)
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.