Flask url 参数捕获

Flask url parameter capture

import flask

app = flask.Flask(__name__)

@app.route("/")
def index():
    return "You need to login"

@app.route('/login', methods=['GET', 'POST'])
def login():
    query = flask.request.query_string   # query is printed in terminal
    login = flask.request.get('login')   # here getting AttributeError
    print query, login
    if flask.request.method == 'POST':
        return "User is {}, Password is {}".format(flask.request.form['login'],flask.request.form['password']) 
    else:
        return flask.render_template("login.html")


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

当我在浏览器 http://127.0.0.1:5000/login?login=hello&password=1234 中传递 url 时,它显示:

AttributeError: 'Request' object has no attribute 'get'

报错正确,Flaskrequest object没有这个方法

也许您想在 request.args or request.values MultiDict object instead? MultiDict objects do have a .get() method:

上使用它
login = flask.request.args.get('login')

如果你想用相同的键获取 MultiDict 中的所有值,那么你应该使用 list_of_values = flask.request.args.getlist('name_of_key').