In python flask, how do you get the path parameters outside of the route function?

Question:

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?

Asked By: yigal

||

Answers:

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']
Answered By: julienc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.