Answer the question
In order to leave comments, you need to log in
What is wrong with uploading an image?
admin.py has a method:
def save_model(self, request, obj, form, change):
obj.save()
new_obj = Product.objects.latest('id')
image = request.FILES
Upload.upload_files(image, 'product', new_obj.id)
class Upload:
def upload_files(files, folder_name, id):
print(files)
id = str(id)
path = MEDIA_ROOT + '/images/' + folder_name + '/' + id
if not os.path.exists(path):
os.makedirs(path)
for image in files:
print(image)
extensions = str(image).split('.')
millis = str(round(time.time() * 1000))
def process(f):
with open(path + '/' + millis + extensions[-1],
'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
process(image)
<MultiValueDict: {'image': [<InMemoryUploadedFile: bicycle_wheel_drops_blur_101128_1440x900.jpg (image/jpeg)>, <InMemoryUploadedFile: change_admin.png (image/png)>]}>
image
Answer the question
In order to leave comments, you need to log in
You have the string "image" in the image, because there is an error in iterating over the dictionary in the for loop:
for image in files:
...
for key, image in files:
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question