flask鉤子函數

來源:互聯網
上載者:User

標籤:就是   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鉤子函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.