如何从端点/规则获取视图功能? [英] How can I get the view function from an endpoint/rule?

查看:163
本文介绍了如何从端点/规则获取视图功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flask(或werkzeug)中,当我拥有的是规则时,如何获得视图函数? (或该规则的端点)?

解决方案

Werkzeug仅存储规则映射,每个规则都有一个端点。通过将每个端点与一个函数相关联,Flask添加到Werkzeug中。使用 app.view_functions dict从端点名称获取视图函数。

 #假设r是规则
f = app.view_functions [r.endpoint]


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

解决方案

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]

这篇关于如何从端点/规则获取视图功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆