quart.helpers 模块#
- quart.helpers.abort(code: int | Response, *args: Any, **kwargs: Any) NoReturn #
为给定的状态码引发 HTTPException。
- quart.helpers.find_package(name: str) tuple[Path | None, Path] #
查找包的安装前缀(或 None)及其包含的文件夹
- async quart.helpers.flash(message: str, category: str = 'message') None #
向会话存储添加消息(带可选类别)。
这通常用于向用户闪现一条消息,该消息将存储在会话中并在其他请求期间显示。例如,
@app.route('/login', methods=['POST']) async def login(): ... await flash('Login successful') return redirect(url_for('index'))
允许索引路由显示闪现的消息,而无需将消息作为参数传递或以其他方式进行处理。有关消息检索,请参阅
get_flashed_messages()
。
- quart.helpers.get_debug_flag() bool #
读取 QUART_DEBUG 环境变量以确定是否在调试模式下运行应用程序。如果未设置,并且已配置开发模式,它将自动启用。
- quart.helpers.get_flashed_messages(with_categories: bool = False, category_filter: Iterable[str] = ()) list[str] | list[tuple[str, str]] #
检索存储在会话中的闪现消息。
这在模板中非常有用,在模板中它被公开为全局函数,例如
<ul> {% for message in get_flashed_messages() %} <li>{{ message }}</li> {% endfor %} </ul>
请注意,使用
category_filter
需要谨慎,因为所有消息都将弹出,但只有与过滤器匹配的消息才会返回。有关消息创建,请参阅flash()
。
- quart.helpers.get_load_dotenv(default: bool = True) bool #
获取用户是否通过设置
QUART_SKIP_DOTENV
禁用加载默认 dotenv 文件。默认值为True
,加载文件。:param default: 如果环境变量未设置,则要返回的值。
- quart.helpers.get_template_attribute(template_name: str, attribute: str) Any #
从模板加载属性。
这在 Python 代码中很有用,以便在模板中使用属性。
- 参数:
template_name – 加载属性的来源。
attribute – 要加载的属性名称
- async quart.helpers.make_push_promise(path: str) None #
创建推送承诺,这是一个简单的包装函数。
如果协议为 HTTP/2,这将获取应推送到客户端的路径。
- async quart.helpers.make_response(*args: Any) Response | Response #
创建一个响应,这是一个简单的包装函数。
当您想在返回响应之前修改响应时,这非常有用,例如
response = make_response(render_template('index.html')) response.headers['X-Header'] = 'Something'
- quart.helpers.redirect(location: str, code: int = 302) Response #
使用状态码重定向到位置。
- async quart.helpers.send_file(filename_or_io: bytes | str | PathLike | BytesIO, mimetype: str | None = None, as_attachment: bool = False, attachment_filename: str | None = None, add_etags: bool = True, cache_timeout: int | None = None, conditional: bool = False, last_modified: datetime | None = None) Response #
返回一个用于发送给定文件名的响应。
- 参数:
filename_or_io – 要发送的文件名(路径),请记住使用
safe_join()
。mimetype – 要使用的 MIME 类型,默认情况下将猜测或恢复为 DEFAULT_MIMETYPE。
as_attachment – 如果为真,请在 Content-Disposition 附件标题中使用附件文件名。
attachment_filename – 文件名的名称,如果不同。
add_etags – 根据文件名、大小和修改时间设置 etags。
last_modified – 用于覆盖上次修改的值。
cache_timeout – 响应被缓存的时间(以秒为单位)。
- async quart.helpers.send_from_directory(directory: bytes | str | PathLike, file_name: str, *, mimetype: str | None = None, as_attachment: bool = False, attachment_filename: str | None = None, add_etags: bool = True, cache_timeout: int | None = None, conditional: bool = True, last_modified: datetime | None = None) Response #
从给定目录发送文件。
- 参数:
directory – 与 file_name 结合后给出文件路径的目录。
file_name – 与 directory 结合后给出文件路径的文件名。
有关其他参数,请参阅
send_file()
。
- quart.helpers.stream_with_context(func: Callable) Callable #
与生成器共享当前请求上下文。
这允许在流式生成器中访问请求上下文,例如,
@app.route('/') def index() -> AsyncGenerator[bytes, None]: @stream_with_context async def generator() -> bytes: yield request.method.encode() yield b' ' yield request.path.encode() return generator()
- quart.helpers.url_for(endpoint: str, *, _anchor: str | None = None, _external: bool | None = None, _method: str | None = None, _scheme: str | None = None, **values: Any) str #
返回特定端点的 URL。
这在模板和重定向中最有用,用于创建可以在浏览器中使用的 URL。
- 参数:
endpoint – 用于构建 URL 的端点,如果以
.
为前缀,则目标端点位于当前蓝图中。_anchor – 要附加的额外锚文本(即 #text)。
_external – 返回用于外部(应用程序)使用的绝对 URL。
_method – 要与端点一起考虑的方法。
_scheme – 要使用的特定方案。
values – 用于构建到 URL 中的值,如端点规则中指定。