When GET was trying to find a file named DS C0001, this was the error code:

INFO: - - [24/May/2021 13:14:10] "GET /static/images/DS HTTP/1.0" 404 -

I could simply reject all files with names that have spaces in them. But this doesn't seem very user-friendly. Does anyone know how to make HTML know that the white spaces are not a sign to stop the string?

This is my HTML code

    <a href={{'/static/images/'+ filename}}>{{filename}}</a>
    <img src={{'/static/images/'+ filename}} alt={{filename}}>

This is the route that leads to it

@app.route("/pconvertedImage", methods=['GET', 'POST'])
def pconvertedImage():
    path = os.path.join(app.config["IMAGE_UPLOADS"], filename)
    old_image = Image.open(open(path,'rb'))
    old_size = (old_image.width, old_image.height)
    tuple_color = hex_to_rgb(hexcolor)
    #remember this is for 4:5 landscape
    new_width = old_image.width * ((100+multiplier) / 100)
    new_size = (round(new_width), round(new_width * 5/4))

    new_image = Image.new("RGB", new_size, tuple_color)
    new_image.paste(old_image, ((new_size[0]-old_size[0])//2, (new_size[1]-old_size[1])//2))
    new_image.save(os.path.join(app.config["IMAGE_CONVERTED"], filename))

    return render_template('viewImage.html', new_image=new_image, filename = filename)
  • Take a look at cs50.stackexchange.com/questions/41141/… . This should help.
    – Vsjain
    May 25 at 11:19
  • yep i worked around the issue by just saving the file with underscores instead of spaces but I would still like to know how to deal with filenames that have spaces. Thank you!
    – Melvin
    May 25 at 12:16

IME files names that include spaces will only lead to sleepless nights and heartbreak.

Inspect the anchor element(s) in the browser. The href argument is already wrong. Remember, the attribute arguments should (must?) be enclosed in delimiters, preferably ". Does the rendered HTML validate?

Add the delimiters the the href attribute(s) (and img src= while you're at it).

NB it still will not validate, but it worked in a repro using firefox.

  • thank you so much for this. I was under the impression that by enclosing it in " " the href and src would take the {{jinja arguments}} as part of the string instead of actual code.
    – Melvin
    May 25 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .