Flask——請求+響應+消息閃爍

目錄

  1. 請求
  2. 響應
  3. 消息閃爍

 

  1. 請求


  2. 響應


    # 使用return直接響應字符串
    @app.route('/')
    def index():
     return 'Hello Flask!'
     return 'Hello Flask!',404 #響應內容並響應狀態碼
    
    通過make_response構造響應
    導入:
    from flask import make_reponse
    @app.route('/')
    def index():
     return make_response('Hello Flask!')
     return make_response('Hello Flask!',404) #響應內容並響應狀態碼

     

  3. 消息閃爍


    # 配置
    app.config["SECRET_KEY"] = "qieqw0923-0-klzd//../op[p%^]"
    # 使用
    在需要彈出消息時,使用 flash 函數保存閃爍消息
    @app.route('/')
    def index():
     flash('hello')
     return render_template('index.html')
    # 在模板文件中顯示閃爍消息時,可以通過函數 get_flashed_messages 獲取
    # 從bootstrap上粘貼1個可消失的警告框
    {% for message in get_flashed_messages() %}
     <div class="alert alert-warning alert-dismissible"
    role="alert"> <button type="button" class="close" data-dismiss="alert" arialabel="Close"><span aria-hidden="true">&times;</span>
    </button>
     {{ message }}
     </div>
    {% endfor %}

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章