- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑以下代码
import flask
class API:
def hello(self):
return flask.Response('hello', 200)
api = API()
app = flask.Flask(__name__)
app.add_url_rule('/', 'hello', api.hello)
app.run()
它在 GET
调用 /
时返回“hello”。
documentation for add_url_rule
指出
[
add_url_rule
] works exactly like theroute()
decorator.
但是它至少需要三个参数。第一个和第三个是可以理解的并且模仿了@route()
。第二个是什么(hello
在我的例子中)?
文档进一步说明这是
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
这是什么意思?为什么 URL (/
) 和调用方法 (api.hello
) 不够用? “端点”的作用是什么?具体怎么用?
最佳答案
这是路线的名称;您将在 url_for()
function 中使用的那个例如。端点名称是 View 的注册 key ,是一个符号名称,您可以通过它从应用程序的其他部分引用路由。
@route()
采用相同的参数;默认是装饰函数的名称。这在 add_url_rule()
文档以及 @route()
的文档中都有记录。 :
- endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint.
(粗体斜体强调我的)。
请注意,文档中的示例试图显示相同的内容:
Basically this example:
@app.route('/')
def index():
passIs equivalent to the following:
def index():
pass
app.add_url_rule('/', 'index', index)
请注意,第二个参数 'index'
与函数名称匹配。
关于python - flask 的 .add_url_rule() 中的 "endpoint"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607711/
我正在尝试将 Flask 服务器包装在一个类中,以便更好地适应应用程序其余部分的结构。 我有以下代码: class HTTPServer(object): def __init__(self)
我的 Flask 路由中有一堆装饰器,我试图将它们压缩成一个(包括 @app.route)。 我有以下@route函数: from functools import wraps def route(r
我正在尝试用 Flask 编写一个服务器。 我想使用 HTTP 方法名称以外的名称路由到类的方法,但是如果使用下面的源代码,则会发生错误。 from flask import Flask app =
使用 add_url_rule 失败,但 @app.route('') 工作正常。 您好,我正在尝试将 SSE(服务器发送事件)添加到 Python Flask 应用程序中。不幸的是,该应用程序使用
我在 Flask 中使用基于类的 View 来创建 CRUD REST API 并使用 add_url_rule 注册路由,就像这样... class GenericAPI(MethodView):
来自 Flask 文档: def index(): pass app.add_url_rule('/', 'index', index) 它还说: endpoint – the endpoin
我正在使用以下设置 url 端点: manager.py from xxx import ContactAPI from xxx.models import Contact # self.app
考虑以下代码 import flask class API: def hello(self): return flask.Response('hello', 200) api
我在 Heroku 上使用 Gunicorn 来尝试提供一个基本网页,如果我使用普通的路由装饰器,它工作正常。例如: from flask import Flask app = Flask(__nam
我是一名优秀的程序员,十分优秀!