Not obvious from the flask documention on how to get the query string. I am new, looked at the docs, could not find!
So
@app.route('/')
@app.route('/data')
def data():
query_string=??????
return render_template("data.html")
Not obvious from the flask documention on how to get the query string. I am new, looked at the docs, could not find!
So
@app.route('/')
@app.route('/data')
def data():
query_string=??????
return render_template("data.html")
The full URL is available as
request.url
, and the query string is available asrequest.query_string
.Here's an example:
To access an individual known param passed in the query string, you can use
request.args.get('param')
. This is the "right" way to do it, as far as I know.ETA: Before you go further, you should ask yourself why you want the query string. I've never had to pull in the raw string - Flask has mechanisms for accessing it in an abstracted way. You should use those unless you have a compelling reason not to.
We can do this by using request.query_string.
Example:
Lets consider view.py
You also make it more modular by using Flask Blueprints - http://flask.pocoo.org/docs/0.10/blueprints/
Lets consider first name is being passed as a part of query string /web_url/?first_name=john
As you see this is just a small example - you can fetch multiple values + formate those and use it or pass it onto the template file.
Werkzeug/Flask as already parsed everything for you. No need to do the same work again with urlparse:
The full documentation for the request and response objects is in Werkzeug: http://werkzeug.pocoo.org/docs/wrappers/