測開之路一百二十八:flask之重定向和404


 

 

a、b兩個視圖,分別返回a的頁面和b的頁面

 

 

 

 

重定向:redirect

重定向到路由:請求/a/時,重定向到/b/

 

 

 

重定向到視圖函數:url_for(“函數名“),訪問/a/時,重定向到函數b()

 

 

 
        

主動返回404:abort

 

 

 

# coding:utf-8
from flask import Flask, render_template, redirect, url_for, abort

app = Flask(__name__)


# 訪問/help/時主動返回404
@app.route('/help/')
def req_help():
abort(404)


@app.route("/a/")
def a():
# return render_template("a.html")
# return redirect('/b/')
return redirect(url_for('b')) # 根據視圖函數名稱綁定路由地址


@app.route("/b/")
def b():
return render_template("b.html")


if __name__ == '__main__':
app.run(debug=True)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM