pythonhtmlflask

Flask "render_template" returns old html page


I use Flask and python. I have updatable .html file in my templates folder.

For updating .html file I delete it before rendering and then write it again.

if os.path.isfile(html_file):
    os.unlink(html_file)

html_text = ['bla', 'bla'] # that's the part that changes every time

with open(html_file, 'w') as f:
    f.writelines(html_text)

File is deleted, then created and written properly.

Then I call it:

@app.route('/html_file')
def show_html_file():
    return render_template('html_file.html')

First time render_template returns proper file. But then, not matter: updated .html file or even deleted, route /html_file show only old version of file. I've tried to force browser to force refresh ignoring caching, tried different browsers. I still got first version of .html file.

How to fix it?


Solution

  • It looks like templates by default are not updatable. And it must be forced.

    Here is a way how to do it:

    app = Flask(__name__)
    app.config['TEMPLATES_AUTO_RELOAD'] = True