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'))
相關文章
相關標籤/搜索