Search code examples
pythonflaskwerkzeug

How can I get the view function from an endpoint/rule?


In Flask(or werkzeug), how can I get the view function when all I have is the Rule? (or the Endpoint from that rule)?


Solution

  • Werkzeug only stores a map of rules, each of which has an endpoint. Flask adds to Werkzeug by associating each endpoint with a function. Use the app.view_functions dict to get the view function from the endpoint name.

    # assuming r is a Rule
    f = app.view_functions[r.endpoint]