'str' object has no attribute 'chunks'

I have this error when try to save image in folder and in mongo db how i can solve it

def save_img_field(value):

    # Save image file to the static/img folder
    image_path = save_image_to_folder(value)

    # Save image to MongoDB using GridFS
    image_id = fs.put(value, filename=value, content_type=value.content_type)

    # Return the image id and path for storage in MongoDB and Django folder
    return {'id': str(image_id), 'path': image_path}

def save_image_to_folder(value):
    # Create the file path to save the image in the Django static/img folder
    image_name = value
    image_path = f'decapolis/static/img/{image_name}'

    # Open the image file and save it to the folder
    with open(image_path, 'wb+') as destination:
        for chunk in value.chunks():

    # Return the image path
    return image_path

am try to solve it in many way but not working

While saving to a file, add check wheter it is a string or an image:

with open(image_path, 'wb+') as destination:
    if type(value) == str:
        destination.write(value)   # or whatever you actually want to do if it's a string
        for chunk in value.chunks():
Back to Top