Forums

Wrong image displayed

I'm trying to display an image to a website using flask.

I have a scheduled task that creates the image file for me and puts it into

mysite/static/image.png

I then have an html template with the following code for the image

<img src="{{url_for('static', filename='image.png')}}"/>

When I visit the site, it is displaying an old version of the image that is no longer located anywhere in my directories(or my pythonanywhere account for that matter).

Can anyone assist? Also can I assume that when my scheduled task runs and updates this file that the website will automatically reflect the changes without having to reload the web app?

Thanks

Welp I see now it has to do with the image being cached by browser

Trying to fix that now. This code in the main flask_app.py file should work?

resp = make_response()
resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"

it might! But unless you specifically don't want the image to be cached for anyone ever, you could just clear your local browser cache? shift+reload or a private browsing session should do it...

Yeah I don't want the image to be cached ever.

The above was not working for some reason but I got the following to work

@app.after_request
def add_header(response):
    response.headers['Pragma'] = 'no-cache'
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    response.headers['Expires'] = '0'
return response