Flask url参数捕获

时间:2015-01-09 22:10:34

标签: python flask

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中传递网址时,它会说:

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

2 个答案:

答案 0 :(得分:1)

错误消息是正确的,Flask request object没有这样的方法。

也许您想在request.argsrequest.values MultiDict对象上使用它? MultiDict个对象有一个.get() method

login = flask.request.args.get('login')

答案 1 :(得分:0)

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