我已經看到了兩種不同的方式來處理路由上的 post 請求:
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
# insert logic on data
return render_template('register.html', title="Register", form=form)
和:
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# insert logic
我似乎無法找到以下問題的明確答案,而且我似乎無法在 SO 中找到重復項:
- 它們是否指代不同的用例?
- 兩者都應該使用嗎?它們是多余的嗎?
提前致謝!
uj5u.com熱心網友回復:
您的第一個方法將在任一情況下執行:“用戶請求資料 [GET]”或“用戶發布表單 [POST]”。如果要將 GET 和 POST 請求都分配給同一路由,請使用第二種方法。
請參閱下面的代碼:
if request.method == "POST":
# HTTP Method POST. That means the form was submitted by a user
# and we can find her filled out answers using the request.POST QueryDict
else:
# Normal GET Request (most likely).
# We should probably display the form, so it can be filled
# out by the user and submitted.
或者您將想要創建不同的路由功能,一個會發布您的表單,另一個會在您發布資料后重定向您以獲取功能,如果這是您正在尋找的。
@app.route('/register', methods=['POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
# redirect to '/register_success' with user data
@app.route('/register_success',methods=['GET'])
def registration_success():
# render <You are registered 'user'> --etc--
uj5u.com熱心網友回復:
GET 方法不應用于傳輸敏感資訊(如密碼),因為 GET 在 URL 中對表單資料進行編碼,而 POST 資料在請求正文中發送。W3Schools對 GET 和 POST 方法進行了相當簡單的比較,進一步解釋了兩者之間的差異。請注意,POST 不保證安全性,但 GET確保缺乏安全性。
至于使用 Flask 實作,您可以使用不同的 URL(一個用于獲取注冊表單,另一個用于將資料發送回服務器),但您也可以使用相同的 URL,并以不同的方式處理每個請求。最重要的是確保您的客戶端代碼(HTML)使用 POST 而不是 GET 將資料發送到服務器。
以下是一些代碼,我希望能證明它們的區別。瀏覽兩者/register并且/reg_with_method應該適用于“注冊流程”。但是,提交表單/register_with_get應該讓您知道密鑰現在在地址欄中。
from flask import Flask, request
app = Flask(__name__)
registration_form = '''
<html>
<head><title>Registeration form</title></head>
<body>
{additional_message}
<form action="{location}" method="{method}">
<input type="text" name="username" value="UserName"><br>
<input type="text" name="registration_key" value="Key"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
'''
registration_success = '''
<html>
<head><title>Registeration complete</title></head>
<body>
Successfully registerd user {username}.
</body>
</html>
'''
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
return post_registration_form(location="/register")
# default to GET, but you can add other methods or error handling
return get_registration_form(location="/register")
@app.route("/register_with_get")
def register_get_only():
if request.args.get("registration_key"): # The key is now in the address bar
return get_registration_form(location="/register", additional_message="SECURITY: The SIKRIT key is now in the address bar. Now we will use POST.<br>")
return get_registration_form(location="/register_with_get", method="get") # This is a BAD IDEA, the key will be in the address bar.
@app.get("/reg_with_method")
def get_registration_form(location = "/reg_with_method", additional_message = "", method = "post"):
return registration_form.format(location=location, additional_message=additional_message, method=method)
@app.post("/reg_with_method")
def post_registration_form(location = "/reg_with_method"):
if request.form.get("registration_key") != "SIKRIT":
return get_registration_form(location=location, additional_message="Bad sikrit key! try again..<br>")
return registration_success.format(username=request.form.get("username"))
if __name__ == "__main__":
app.run()
注意:這是一個過于簡化的版本。這不是一個合適的 Flask 應用程式的撰寫方式,但我選擇避免使用模板和回應物件使事情復雜化,以專注于手頭的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/532106.html
標籤:Python烧瓶烧瓶-wtforms烧瓶宁静的烧瓶登录
