kramer65 kramer65 - 2 months ago 21
Python Question

Python: How to get multiple variables from a URL in Flask?

I'm trying to get multiple arguments from a url in Flask. After reading this SO answer I thought I could do it like this:

@app.route('/api/v1/getQ/', methods=['GET'])
def getQ(request):
print request.args.get('a')
print request.args.get('b')
return "lalala"

But when I visit
, I get a
TypeError: getQ() takes exactly 1 argument (0 given)
. I tried other urls, like
, but to no avail.

Does anybody know what I'm doing wrong here? All tips are welcome!


You misread the error message; the exception is about how getQ is called with python arguments, not how many URL parameters you added to invoke the view.

Flask views don't take request as a function argument, but use it as a global context instead. Remove request from the function signature:

from flask import request

@app.route('/api/v1/getQ/', methods=['GET'])
def getQ():
    print request.args.get('a')
    print request.args.get('b')
    return "lalala"

Your syntax to access URL parameters is otherwise perfectly correct. Note that methods=['GET'] is the default for routes, you can leave that off.