-1

I am trying to get the images uploaded to the form by the user to then be stored in the original static file. However, once the user uploads them, they are stored in a new static file rather than the original one created.

File structure

  • Shop
    • baskets
    • goods
    • owner
    • static (where I want the images to go)
      • styles.css
    • templates
    • init.py
  • static (where they are instead going)
    • images
  • app.py

init.py

app.config['UPLOADED_IMAGES_DEST'] = 'static/images'
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

The form and actual uploading of the images works fine, it's just they are being uploaded in the wrong location so when I try and reference the image in HTML using Jinja it does not work.

I have tried watching videos and know its something to do with the first line in init.py however am not sure how to get the directory that would link to the original static file.

How would I go about uploading it to the static file within Shop instead? Thanks in advance.

abcdefg
  • 1
  • 3
  • It looks like you need to add the shop to the path variable. Something like: app.config['UPLOAD_IMAGES_DEST'] = SHOP_PATH + '/static/images' (replace SHOP_PATH with the actual path) – ChrisSc Jan 04 '23 at 19:29

1 Answers1

0

When you executed app.py and gave the path static/images, you are giving a relative path that depends on where the python script is being executed. In this case, it would be LOCATION_OF_APP.PY/static/images and indeed it is where the images are being sent to.

To make the images go to the correct destination, it would be:

import os
app.config['UPLOADED_IMAGES_DEST'] = os.path.join(os.path.dirname(__file__), 'Shop/static/images')

The os module's dirname method returns the directory name of app.py and join method concatenates the two strings in a way that works for both unix and windows systems

Eduardo Matsuoka
  • 566
  • 6
  • 18
  • Hi thank you very much for the response. After trying this method it now creates a folder called shop with the folders static and images within the shop folder and then uploads the images there rather than the original static folder. – abcdefg Jan 04 '23 at 20:48
  • os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static/images') – abcdefg Jan 04 '23 at 21:10