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

本文翻译自:How can I get the named parameters from a URL using Flask?

When the user accesses this URL running on my flask app, I want the web service to be able to handle the parameters specified after the question mark: 当用户访问在我的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)

#1楼

参考:https://stackoom.com/question/1gRYR/如何使用Flask从URL获取命名参数


#2楼

Use request.args to get parsed contents of query string: 使用request.args获取查询字符串的解析内容:

from flask import request

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

#3楼

The URL parameters are available in request.args , which is a MultiDict that has a get method, with optional parameters for default value ( default ) and type ( type ) - which is a callable that converts the input value to the desired format. URL参数可在request.args中使用, request.args是一个具有get方法的MultiDict ,具有用于默认值( 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)

Examples with the code above: 上面的代码示例:

/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: '*'

#4楼

You can also use brackets <> on the URL of the view definition and this input will go into your view function arguments 您还可以在视图定义的URL上使用方括号<>,此输入将进入您的视图函数参数

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

#5楼

If you have a single argument passed in the URL you can do it as follows 如果您在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)

In case you have multiple parameters: 如果您有多个参数:

#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)

What you were trying to do works in case of POST requests where parameters are passed as form parameters and do not appear in the URL. 在POST请求的情况下,您尝试执行的操作将参数作为表单参数传递并且不出现在URL中。 In case you are actually developing a login API, it is advisable you use POST request rather than GET and expose the data to the user. 如果您实际上正在开发登录API,建议您使用POST请求而不是GET,并将数据公开给用户。

In case of post request, it would work as follows: 如果有职位要求,它将按以下方式工作:

#url
http://10.1.1.1:5000/login

HTML snippet: HTML片段:

<form action="http://10.1.1.1:5000/login">
  Username : <input type="text" name="username"><br>
  Password : <input type="password" name="password"><br>
  <input type="submit" value="submit">
</form>

Route: 路线:

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)

#6楼

url: 网址:

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

code: 码:

@app.route('/user/< string:name >/', methods=['GET', 'POST'])
def user_view(name):
    print(name)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值