In flask, you can define path parameters like so:
@app.route('/data/<section>') def data(section): print section
In The above example, you can access the section variable only from the data endpoint (unless you pass it around in function parameter)
You can also get the query parameters by accessing the request object. this works from the endpoint function as well as any other called function, without needing to pass anything around
request.args['param_name']
my question is: is in possible to access the path parameter (like section above) in the same way as the query parameters?
The link to route /b in the template for /a could have query parameters added to it, which you could read in the route for /b . Alternatively you could store the value for a in a session variable to access it between views.
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.
By default, a route only answers to GET requests. You can use the methods argument of the route() decorator to handle different HTTP methods. If GET is present, Flask automatically adds support for the HEAD method and handles HEAD requests according to the HTTP RFC.
It's possible to use request.view_args
. The documentation defines it this way:
A dict of view arguments that matched the request.
Here's an example:
@app.route("/data/<section>") def data(section): assert section == request.view_args['section']
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