如何使用 Flask 从 URL 获取命名参数? [英] How can I get the named parameters from a URL using Flask?

查看:35
本文介绍了如何使用 Flask 从 URL 获取命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问在我的flask应用程序上运行的这个URL时,我希望web服务能够处理问号后指定的参数:

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:

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)

推荐答案

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

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

from flask import request

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

这篇关于如何使用 Flask 从 URL 获取命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆