How to write the flask app.route
if I have multiple parameters in the URL call?
Here is my URL I am calling from AJax:
http://0.0.0.0:8888/createcm?summary=VVV&change=Feauure
I was trying to write my flask app.route
like this:
@app.route('/test/<summary,change>', methods=['GET']
But this is not working. Can anyone suggest me how to mention the app.route
?
To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string. @app. route('/createcm/<summary>/<change>') def createcm(summary=None, change=None): #... to create the createcm view function by using the app.
In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page/response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function. The code below shows how you use the a function for multiple URLs.
These parameters get bonded to the URL. When there is an URL that is built for a specific function using the url_for( ) function, we send the first argument as the function name followed by any number of keyword argument. Each of the keyword argument corresponds to a variable part of the URL.
Yes, deploy your application on a different WSGI server, see the Flask deployment options documentation. The server component that comes with Flask is really only meant for when you are developing your application; even though it can be configured to handle concurrent requests with app.
The other answers have the correct solution if you indeed want to use query params. Something like:
@app.route('/createcm') def createcm(): summary = request.args.get('summary', None) change = request.args.get('change', None)
A few notes. If you only need to support GET requests, no need to include the methods in your route decorator.
To explain the query params. Everything beyond the "?" in your example is called a query param. Flask will take those query params out of the URL and place them into an ImmutableDict. You can access it by request.args
, either with the key, ie request.args['summary']
or with the get method I and some other commenters have mentioned. This gives you the added ability to give it a default value (such as None), in the event it is not present. This is common for query params since they are often optional.
Now there is another option which you seemingly were attempting to do in your example and that is to use a Path Param. This would look like:
@app.route('/createcm/<summary>/<change>') def createcm(summary=None, change=None): ...
The url here would be: http://0.0.0.0:8888/createcm/VVV/Feauure
With VVV and Feauure being passed into your function as variables.
Hope that helps.
You can try this:
curl -i "localhost:5000/api/foo?a=hello&b=world"
from flask import Flask, request app = Flask(__name__) @app.route('/api/foo/', methods=['GET']) def foo(): bar = request.args.to_dict() print bar return 'success', 200 if __name__ == '__main__': app.run(debug=True)
{'a': u'hello', 'b': u'world'}
P.S. Don't omit double quotation(" ") with curl option, or it not work in Linux cuz "&"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With