I have 2 ways to upload an image, one via files (image
) and one via url (imageURL
). If the user chooses url, I have a model method to download the file from the url:
class Post(models.Model):
...
image = models.FileField(null=True, blank=True)
imageURL = models.URLField(null=True, blank=True)
def download_file_from_url(self):
print('DOWNLOAD') #prints
# Stream the image from the url
try:
request = requests.get(self, stream=True)
except requests.exceptions.RequestException as e:
# TODO: log error here
return None
if request.status_code != requests.codes.ok:
# TODO: log error here
return None
# Create a temporary file
lf = tempfile.NamedTemporaryFile()
# Read the streamed image in sections
for block in request.iter_content(1024 * 8):
# If no more file then stop
if not block:
break
# Write image block to temporary file
lf.write(block)
print('WRITE') #prints
return files.File(lf)
views
def post(request):
...
if form_post.is_valid():
instance = form_post.save(commit=False)
if instance.imageURL:
instance.image = Post.download_file_from_url(instance.imageURL)
instance.save()
Uploading an image from file works fine..however when uploading via url, the model method returns this error:
SuspiciousFileOperation at /post/
The joined path (/var/folders/t9/7v8mki3s3h39fzylxfpsk9640000nn/T/tmpmvb9wxq6) is located outside of the base path component (/Users/zorgan/Desktop/app/draft1/media)
Can someone explain what this means? It's obviously not uploading to the media
folder, but I have no idea what /var/folders/
is. All my media files upload to the stardard media
folder:
urls
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and this works fine when uploading via files (image
). It only returns the error when uploading via url imageURL
. Any idea what the problem is?