源碼:
from flask import Flask,redirect,url_for
app = Flask(__name__)
@app.route("/home")
def home():
return"Hello! This is the main page <h1>HELLO<h1/>"
@app.route("/<name>")
def user(name):
return f"Hello {name}!"
@app.route('/admin')
def admin():
return redirect(url_for("home"))
if __name__ == "__main__":
app.run(debug=True)
學(xué)習(xí)來源:https://www.bilibili.com/video/BV1Qv4y1Z7jD
run flask項(xiàng)目:在flask項(xiàng)目的路徑下進(jìn)入terminal,python 該flask項(xiàng)目。
或者vscode中Ctrl+option+N 運(yùn)行該項(xiàng)目。
或者sublime text中command+B 運(yùn)行該項(xiàng)目。
debug=True 設(shè)置后需要重啟terminal
redirect 重定向
url_for()的作用:
(1)給指定的函數(shù)構(gòu)造 URL。
(2)訪問靜態(tài)文件(CSS / JavaScript 等)。 只要在你的包中或是模塊的所在目錄中創(chuàng)建一個(gè)名為 static 的文件夾,在應(yīng)用中使用 /static 即可訪問。
聯(lián)系客服