A
A
Aslamb2019-02-16 15:48:09
Python
Aslamb, 2019-02-16 15:48:09

How to update static/clear cache in browser with python-flask?

In general, I changed the pictures from my statics, and the browser shows the old ones.
A little earlier, I had another problem that the styles were not updated and I was offered to add the following code to the Flask file:

#   SNIPPET FOR CAESH   #
@app.context_processor
def override_url_for():
    return dict(url_for=dated_url_for)

def dated_url_for(endpoint, **values):
    if endpoint == 'static':
        filename = values.get('filename', None)
        if filename:
            file_path = os.path.join(app.root_path,
                                     endpoint, filename)
            values['q'] = int(os.stat(file_path).st_mtime)
    return url_for(endpoint, **values)
#   SNIPPET FOR CAESH   #

I have two questions:
  • How in this case to force the browser to update images?
  • How to clear the browser cache in Python?

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pcdesign, 2019-02-16
@pcdesign

@app.after_request
def add_header(response):
    response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
    response.headers['Cache-Control'] = 'public, max-age=0'
    return response

You can disable browser caching.

Z
Zanak, 2019-02-19
@Zanak

Search is your friend. You can force pages to become outdated like this. Hint for puff, but adaptable at times.

R
Roman, 2019-02-25
@skipirich

You can try to cache the view. For example like this

@app.route("/")
@cache.cached(timeout=50)
def index():
    return render_template('index.html')

I really don't know how it will affect the statics)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question