Flask.url_for() error: Attempted to generate a URL without the application context being pushed

后端 未结 3 710
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 10:53

I have a trivial app where I\'m trying to redirect the favicon per:

http://flask.pocoo.org/docs/0.10/patterns/favicon/

app = flask.Flask(__name__)
ap         


        
3条回答
  •  孤城傲影
    2020-12-16 11:15

    Late answer, I've just run into the same problem. I don't see a big downside in handling the redirect like this instead:

    @app.route('/favicon.ico')
    def favicon():
        return redirect(url_for('static', filename='favicon.ico'))
    

    This prevents url_for from being called before the application is ready.

    To give a counterpoint to using a link in the HTML only, it's a good practice for every site to have a favicon.ico and robots.txt at the root level - even if they're empty. It avoids problems like this and other unnecessary errors that adds noise to logs.

提交回复
热议问题