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

时间:2014-07-22 15:49:09

标签: python web-services flask url-parameters

当用户访问我在烧瓶应用程序上运行的此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)

8 个答案:

答案 0 :(得分:431)

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

from flask import request

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

答案 1 :(得分:77)

request.args中提供了网址参数,MultiDict是一个MIME types,其get方法,默认值(default)和类型({的可选参数{1}}) - 可调用,将输入值转换为所需格式。

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)

答案 2 :(得分:36)

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

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

答案 3 :(得分:21)

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

答案 4 :(得分:3)

URL:

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

代码:

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

答案 5 :(得分:1)

这应该有效

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

答案 6 :(得分:0)

这真的很简单。让我将此过程分为两个简单步骤。

  1. 在html模板上,您将用户名和密码的名称标签声明为

    <form method="POST">
    <input type="text" name="user_name"></input>
    <input type="text" name="password"></input>
    </form>
    
  2. 然后,将您的代码修改为:

    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
    

答案 7 :(得分:0)

使用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)

Here is the referenced link to the code.