Hackaholic
Hackaholic

Reputation: 19771

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')

When I pass the url in browser http://127.0.0.1:5000/login?login=hello&password=1234, it says:

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

Upvotes: 0

Views: 414

Answers (2)

rodcoelho
rodcoelho

Reputation: 44

If you want to get all values in the MultiDict with the same key, then you should use list_of_values = flask.request.args.getlist('name_of_key').

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1125398

The error message is correct, the Flask request object has no such method.

Perhaps you wanted to use that on the request.args or request.values MultiDict object instead? MultiDict objects do have a .get() method:

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

Upvotes: 1

Related Questions