Flask url_for generating http URL instead of https

后端 未结 6 2019
醉梦人生
醉梦人生 2020-11-28 07:29

I am using url_for to generate a redirect URL when a user has logged out:

return redirect(url_for(\'.index\', _external=True))

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 07:56

    Setting _scheme on every url_for() call is extremely tedious, and PREFERRED_URL_SCHEME doesn't seem to work. However, mucking with what the request's supposed scheme is at the WSGI level seems to successfully convince Flask to always construct HTTPS URLs:

    def _force_https(app):
        def wrapper(environ, start_response):
            environ['wsgi.url_scheme'] = 'https'
            return app(environ, start_response)
        return wrapper
    
    app = Flask(...)
    
    app = _force_https(app)
    

提交回复
热议问题