標籤:就是 render error temp 地方 class template lis url
@app.context_processordef context_processor(): return {"current_user":"zhiliao"}#這裡必須有傳回值,而且傳回值必須是字典#下面錯誤的寫法@app.context_processordef context_processor_error(): if hasattr(g,‘user‘): return {"current_user":g.user}#不能這樣寫,函數沒有傳回值,預設返回None return {}#這樣寫是正確的
@app.route(‘/‘)def hello_world(): #就是顯示當前app的名字 # print(current_app.name) # print(url_for(‘my_list‘)) # username = request.args.get(‘username‘) # g.username = username #把username綁定到全域變數上,任何地方都可以用 # log_a() # log_b() # log_c() if hasattr(g,‘user‘): print(g.user) abort(404)#如果使用abort這個函數,手動報一個404的錯誤 #然後執行@app.errorhandler(404)這個裝飾下面的代碼 return render_template(‘index.html‘)
@app.errorhandler(500)#用來設定500,404錯誤def server_error(error): return render_template(‘500.html‘)@app.errorhandler(404)def page_no_find(error): return render_template(‘404.html‘)
flask鉤子函數