Python Flask如何从URL获取参数?

时间:2022-02-12 10:30:57

In Flask, How do I extract parameters from a URL? How can I extract named parameters from a URL using flask and python?

在Flask中,如何从URL中提取参数?如何使用flask和python从URL中提取命名参数?

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) 

3 个解决方案

#1


284  

Use request.args to get parsed contents of query string:

使用请求。获取解析查询字符串内容的args:

from flask import request

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

#2


29  

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参数可在请求中使用。args是一个具有get方法的多指令,具有默认值(默认值)和类型(类型)的可选参数,类型(类型)是一个可调用的参数,可以将输入值转换为所需的格式。

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

#3


5  

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

#1


284  

Use request.args to get parsed contents of query string:

使用请求。获取解析查询字符串内容的args:

from flask import request

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

#2


29  

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参数可在请求中使用。args是一个具有get方法的多指令,具有默认值(默认值)和类型(类型)的可选参数,类型(类型)是一个可调用的参数,可以将输入值转换为所需的格式。

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

#3


5  

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