前言
使用 abort() 可以 更早退出请求,并返回错误代码
abort() 函数
使用abort函数可以立即终止视图函数的执行,并可以返回特定的信息
abort(404) # 404 Not Found
abort(Response('Hello World'))
源码介绍
def abort(
status: t.Union[int, "Response"], *args: t.Any, **kwargs: t.Any
) -> "te.NoReturn":
"""Raises an :py:exc:`HTTPException` for the given status code or WSGI
application.
If a status code is given, it will be looked up in the list of
exceptions and will raise that exception. If passed a WSGI application,
it will wrap it in a proxy WSGI exception and raise that::
abort(404) # 404 Not Found
abort(Response('Hello World'))
"""
_aborter(status, *args, **kwargs)
使用示例
from flask import Flask, request, g, abort, Response
app = Flask(__name__)
@app.route("