Reload Flask app when template file changes

前端 未结 10 1500
栀梦
栀梦 2020-11-27 13:04

By default, when running Flask application using the built-in server (Flask.run), it monitors its Python files and automatically reloads the app if its code cha

相关标签:
10条回答
  • 2020-11-27 13:34

    For me works just fine:

     from flask import Flask, render_template, request, url_for, redirect
     app = Flask(__name__)
     app.config["TEMPLATES_AUTO_RELOAD"] = True
    

    See more on http://flask.pocoo.org/docs/1.0/config/

    0 讨论(0)
  • 2020-11-27 13:34

    See http://flask.pocoo.org/docs/1.0/quickstart/ and use FLASK_ENV=development

    0 讨论(0)
  • 2020-11-27 13:35

    Actually for me TEMPLATES_AUTO_RELOAD = True does not work (0.12 version). I use jinja2 and what i have done:

    1. Create function before_request

      def before_request():
          app.jinja_env.cache = {}
      
    2. Register it in application

      app.before_request(before_request)
      
    3. That's it.

    0 讨论(0)
  • 2020-11-27 13:43

    What worked for me is just adding this:

    @app.before_request
    def before_request():
        # When you import jinja2 macros, they get cached which is annoying for local
        # development, so wipe the cache every request.
        if 'localhost' in request.host_url or '0.0.0.0' in request.host_url:
            app.jinja_env.cache = {}
    

    (taken from @dikkini's answer)

    0 讨论(0)
提交回复
热议问题