python flask before_request exclude /static directory python flask before_request exclude /static directory flask flask

python flask before_request exclude /static directory


You'll need to add an Alias or AliasMatch directive to your Apache config file (or .htaccess file, should you not have access to the .conf files) to ensure that Apache serves your static files, rather than Flask. Make sure that you have provided a Directory to allow the Apache web server to access your static path. (Also, don't forget to restart Apache if you are editing the .conf files so your changes will be picked up).

As a temporary stop-gap measure (or to make it easy to work with in development) you could also check to make sure that the string /static/ is not in request.path:

if 'logged_in' not in session \    and request.endpoint != 'login' \    and '/static/' not in request.path:


I think that there is a solution cleaner than checking request.path.

if 'logged_in' not in session and request.endpoint not in ('login', 'static'):    return redirect(url_for('login'))


I do agree with the Apache approach, but for a quick fix I the following logic at the start of the before_request() function:

if flask.request.script_root == "/static":  return