当用户访问我的flask应用程序上运行的这个URL时,我希望web服务能够处理问号后指定的参数:
http://10.1.1.1:5000/login?username=alex&password=pw1
#I just want to be able to manipulate the parameters
@app.route('/login', methods=['GET', 'POST'])
def login():
username = request.form['username']
print(username)
password = request.form['password']
print(password)
这真的很简单。让我把这个过程分成两个简单的步骤。
在html模板中,你将像这样声明用户名和密码的name属性:
<form method="POST">
<input type="text" name="user_name"></input>
<input type="text" name="password"></input>
</form>
然后,像这样修改你的代码:
from flask import request
@app.route('/my-route', methods=['POST'])
# you should always parse username and
# password in a POST method not GET
def my_route():
username = request.form.get("user_name")
print(username)
password = request.form.get("password")
print(password)
#now manipulate the username and password variables as you wish
#Tip: define another method instead of methods=['GET','POST'], if you want to
# render the same template with a GET request too
这真的很简单。让我把这个过程分成两个简单的步骤。
在html模板中,你将像这样声明用户名和密码的name属性:
<form method="POST">
<input type="text" name="user_name"></input>
<input type="text" name="password"></input>
</form>
然后,像这样修改你的代码:
from flask import request
@app.route('/my-route', methods=['POST'])
# you should always parse username and
# password in a POST method not GET
def my_route():
username = request.form.get("user_name")
print(username)
password = request.form.get("password")
print(password)
#now manipulate the username and password variables as you wish
#Tip: define another method instead of methods=['GET','POST'], if you want to
# render the same template with a GET request too