@app.context_processor def context_processor(): return {"current_user":"zhiliao"}#這里必須有返回值,而且返回值必須是字典 #下面錯誤的寫法 @app.context_processor def 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')