0

I want to display images on a webpage I am creating with Flask. I am using a html template so that I can display different images for different pages.

With lots of trial and error, I have found that the only way I can display images is if they are saved in a sub-folder called 'static'. I would like to be able to use relative directories to retrieve images from another directory on my PC.

Why is Flask only allowing me to retrieve images if they are saved in the 'static' folder and is there a way I can retrieve them from elsewhere?

wgb22
  • 247
  • 1
  • 13
  • 1
    You're likely using the default Flask config, which defines `static` as a directory files can be served from. Allowing relative file requests is extremely insecure, as anyone browsing the website could download any file from the server. – clubby789 Oct 11 '19 at 12:21
  • 1
    Static files are the files which can be accessed by your server thus making your files secure, if you want another way you can host them somewhere and add the URL to your html. – Kshitij Saxena Oct 11 '19 at 12:21
  • Thanks for comments. I am new to web development. I didn't think about security concerns when accessing directories – wgb22 Oct 11 '19 at 12:52

1 Answers1

1

Flask needs to know which files it must server as static (css, images, html...) and not as dynamic. Those files should be placed in a folder marked as 'static'. This is a common practice in many API frameworks.

You can specify an different static folder path when you create your flask app:

app = Flask(__name__, static_url_path='/', static_folder='YOUR_STATIC_FOLDER_PATH')
Pelayo Méndez
  • 389
  • 4
  • 10