0

I have created a flask app that accesses images in a folder specified in a config file. For some reason, when I specify the full path to the folder (img_folder: '/user/userName/static/img/') I get a 404 error but when I specify the relative path (img_folder: './static/img/') it works. Looking at my terminal, the 404 error specifies the correct path and file, so the path is certainly correct. What is going on? How can I use the full path without receiving a 404.

This is what it looks like when I specify the full path in the config folder:

img_folder : '/user/userName/static/img/'

This is the HTTP error when I specify the full path in the config:

127.0.0.1 - - [06/Jul/2018 11:26:25] "GET /user/userName/static/img/lion.jpg HTTP/1.1" 404

When I specify the relative path in the config folder like this:

img_folder : './static/img/'

I don't get the 404 error

In my flask app file, I have a route that uses the files from the folder as such:

files = os.listdir(img_folder)
@app.route('/', methods=['GET', 'POST'])
def init():
  return render_template('display.html', file=files[0])

display.html

<!doctype html>
        <div id='img_area'>
            <img src="{{file}}"></img>
        </div>
</html>

So the file is not being displayed when the full path is used, but is being displayed with the relative path.

Sraw
  • 18,892
  • 11
  • 54
  • 87
yalpsid eman
  • 3,064
  • 6
  • 45
  • 71
  • 1
    is the first path the absolute path of the file? (`user/UserName` doesn't look like one) Is the second path you are showing relative to where you ran the flask app (server root)? I'd suggest using `url_for` in your jinja templates, like `{{ url_for('.static', filename=f'img/{filename}') }}`. As @Sraw mentions, url and path are not necessarily the same. – bluesmonk Jul 06 '18 at 16:04
  • 1
    You just confused `url` and `path`. `url` is the path relative to server while `path` is the path relative to file system root. – Sraw Jul 06 '18 at 16:04

0 Answers0