Web Services 如何使用Flask从URL获取命名参数?

8ehkhllq  于 2023-02-13  发布在  其他
关注(0)|答案(8)|浏览(185)

当用户访问在我的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)
eaf3rand

eaf3rand1#

使用request.args获取查询字符串的解析内容:

from flask import request

@app.route(...)
def login():
    username = request.args.get('username')
    password = request.args.get('password')
3df52oht

3df52oht2#

URL参数在request.args中可用,request.args是一个ImmutableMultiDict,它有一个get方法,可选参数为默认值(default)和类型(type),类型(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: '*'
nszi6y05

nszi6y053#

您还可以在视图定义的URL上使用括号〈〉,此输入将进入视图函数参数

@app.route('/<name>')
def my_view_func(name):
    return name
2j4z5cfb

2j4z5cfb4#

如果在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)
kx1ctssn

kx1ctssn5#

网址:

http://0.0.0.0:5000/user/name/

代码:

@app.route('/user/<string:name>/', methods=['GET', 'POST'])
def user_view(name):
    print(name)

(Edit:删除格式字符串中的空格)

epggiuax

epggiuax6#

使用request.args.get(param),例如:

http://10.1.1.1:5000/login?username=alex&password=pw1
@app.route('/login', methods=['GET', 'POST'])
def login():
    username = request.args.get('username')
    print(username)
    password = request.args.get('password')
    print(password)

下面是代码的引用链接。

46qrfjad

46qrfjad7#

这个应该可以

@app.route('/login$username=<username>$password=<password>', methods=['GET', 'POST'])
def login(username, password):
    # you can add stuff
    return f"Username: {username}\nPassword: {password}"
vaqhlq81

vaqhlq818#

其实很简单,让我把这个过程分成两个简单的步骤。
1.在html模板中,您将声明用户名和密码的name属性,如下所示:

<form method="POST">
<input type="text" name="user_name"></input>
<input type="text" name="password"></input>
</form>

1.然后,像这样修改代码:

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

相关问题