Flask下"Uncaught SyntaxError: Unexpected token <",一个有意思的错误

错误描述

@auth_blueprint.before_request
def before_request():
    if current_user.is_authenticated :
        pass
    elif request.path != '/auth/login':
        return redirect(url_for('auth.login'))

当我添加上述代码时,我发现
http://www.test.so/auth/login的第一行报错,查看后没发现问题。上stackoverflow上有人说是错误的把html当作js解析,例如ajax返回值指定出错css

错误排查

后来我在return以前加了一句打印,发现其实不止html文件被重定向了,全部的静态文件都被重定向到http://www.test.so/auth/login,而后,在原html中,他们被指定为js,css,因此解析天然会报错,将代码作了一下调整,解决问题html

@auth_blueprint.before_request
def before_request():
    if current_user.is_authenticated :
        pass
    elif request.path != '/auth/login' and not re.match('/static/', request.path):
        return redirect(url_for('auth.login'))