用上面幾個工具做一個小網站:
代碼如下:
from flask import Flask, request, render_template, redirect
app = Flask(__name__)
@app.route('/')
def index():
# argument , ?wd=python 通過url方式來傳遞參數
wd = request.args.get("wd")
age = request.args.get("age")
print('8'*30)
print(wd)
print(age)
print('8'*30)
return 'Hello World!'
@app.route('/login/', methods=['GET', 'POST'])
def login():
# GET:從服務器上面獲取數據,傳參是通過在url中傳遞的
# POST:在瀏覽器上發送數據到服務器上,傳參是通過表單
if request.method == 'GET':
return render_template('login.html')
else:
telephone = request.form.get('telephone')
password = request.form.get('password')
print('telephone number : %s'% telephone)
print('password : %s'% password)
# redirect 頁面跳轉所用
return redirect('/')
if __name__ == '__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄界面</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="telephone">
<input type="password" name="password">
<button>登錄</button>
</form># 調試很久,出不來post,最後發現是因爲所要表達的部分沒有放在表單form裏面
</body>
</html>
整個過程中需要注意 的問題:
[email protected]('/')相當於 一個索引,引向某一個網頁位置
2.get/post 兩種返值方式
3.表單內的表達函數,必須放在兩個form 之間
4.method用來選擇返值方式
未解決問題(求大牛指點):
用的pycharm建立的flask工程,不知未何出現了運行過程中出現了OSError: [Errno 22] Invalid argument報錯
想了很多種方法,未果,最後,直接重新開機,問題解決.