当用户访问我的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)
URL参数在request中可用。args是一个ImmutableMultiDict,它有一个get方法,有默认值(default)和类型(type)的可选参数——这是一个可调用对象,它将输入值转换为所需的格式。(有关详细信息,请参阅该方法的文档。)
from flask import request
@app.route('/my-route')
def my_route():
page = request.args.get('page', default = 1, type = int)
filter = request.args.get('filter', default = '*', type = str)
上面代码的示例:
/my-route?page=34 -> page: 34 filter: '*'
/my-route -> page: 1 filter: '*'
/my-route?page=10&filter=test -> page: 10 filter: 'test'
/my-route?page=10&filter=10 -> page: 10 filter: '10'
/my-route?page=*&filter=* -> page: 1 filter: '*'
如果你在URL中传递了一个参数,你可以这样做
from flask import request
#url
http://10.1.1.1:5000/login/alex
from flask import request
@app.route('/login/<username>', methods=['GET'])
def login(username):
print(username)
如果你有多个参数:
#url
http://10.1.1.1:5000/login?username=alex&password=pw1
from flask import request
@app.route('/login', methods=['GET'])
def login():
username = request.args.get('username')
print(username)
password= request.args.get('password')
print(password)
在POST请求中,参数作为表单参数传递,而不出现在URL中。在实际开发登录API的情况下,使用POST请求而不是GET请求并将数据公开给用户是明智的。
如有职位要求,其工作方式如下:
#url
http://10.1.1.1:5000/login
HTML代码片段:
<form action="http://10.1.1.1:5000/login" method="POST">
Username : <input type="text" name="username"><br>
Password : <input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
路线:
from flask import request
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
print(username)
password= request.form.get('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
URL参数在request中可用。args是一个ImmutableMultiDict,它有一个get方法,有默认值(default)和类型(type)的可选参数——这是一个可调用对象,它将输入值转换为所需的格式。(有关详细信息,请参阅该方法的文档。)
from flask import request
@app.route('/my-route')
def my_route():
page = request.args.get('page', default = 1, type = int)
filter = request.args.get('filter', default = '*', type = str)
上面代码的示例:
/my-route?page=34 -> page: 34 filter: '*'
/my-route -> page: 1 filter: '*'
/my-route?page=10&filter=test -> page: 10 filter: 'test'
/my-route?page=10&filter=10 -> page: 10 filter: '10'
/my-route?page=*&filter=* -> page: 1 filter: '*'