Skip to content

Commit

Permalink
New translations response.md (Chinese Simplified)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Apr 9, 2024
1 parent 1cf643a commit 6fee283
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions guide/content/zh/guide/basics/response.md
@@ -1,16 +1,16 @@
# 答复
# 响应(Response)

所有 [handlers](./handlers.md) _通常_返回一个响应对象, [middleware](./midd) 可以选择返回一个响应对象
所有 [handlers](./handlers.md) _通常_返回一个响应对象, [middleware](./midd) 可以选择是否返回一个响应对象

2. 澄清该陈述:
解释一下

- unless the handler is a streaming endpoint handling its own pattern for sending bytes to the client, the return value must be an instance of :class:`sanic.response.HTTPResponse` (to learn more about this exception see [streaming responses](../advanced/streaming.md#response-streaming)). 在 **最多** 个案件中,您需要返回响应
- 如果中间件返回响应对象,这将被用来代替处理程序所做的任何(见 [middleware](./midd) 来了解更多)
- 除非处理器是一个流式响应,即发送字节到客户端模式的流式路由,否则返回值必须是:class:`sanic.response.HTTPResponse`类的实例(要了解更多关于这一例外情况,请参[阅流式响应](../advanced/streaming.md#response-streaming)) 否则,您需要返回一个响应
- 如果中间件确实返回了一个响应对象,则会使用该响应对象代替处理器原本的行为(更多细节请参阅 [中间件](./middleware.md) 部分)

最基本的处理程序看起来就像下面一样。 The :class:`sanic.response.HTTPResponse` object will allow you to set the status, body, and headers to be returned to the client.
一个最基本的处理器可能如下所示。 使用 :class:`sanic.response.HTTPResponse` 类对象,您可以设置要返回给客户端的状态码、主体内容以及头部信息。

```python
HTTPResponse, Sanic
from sanic import HTTPResponse, Sanic

app = Sanic("TestApp")

Expand All @@ -19,22 +19,22 @@ def handler(_):
return HTTPResponse()
```

然而,通常较容易使用下文讨论的一种方便方法
然而,通常使用下文讨论的一些便捷方法更为简单

## 方法
## 响应方式(Methods)

生成响应对象的最简单方法是使用一个方便函数
生成响应对象最简便的方式是使用以下便捷函数

### 文本
### Text(文本)

.. :
.. column::

```
**默认内容类型**: `text/plain; charset=utf-8`
**默认Content-Type**: `text/plain; charset=utf-8`
**描述**: 返回纯文本
```

.. :
.. column::

````
```python
Expand All @@ -46,16 +46,16 @@ async def handler(request):
```
````

### HTML
### HTML(HTML)

.. :
.. column::

```
**默认内容类型**: `text/html; charset=utf-8`
**默认Content-Type**: `text/html; charset=utf-8`
**描述**: 返回一个 HTML 文档
```

.. :
.. column::

````
```python
Expand All @@ -67,16 +67,16 @@ async def handler(request):
```
````

### JSON
### JSON(JSON)

.. :
.. column::

```
**默认 Content-Type**: `application/json`
**Description**: 返回 JSON 文档
**描述**: 返回 JSON 数据
```

.. :
.. column::

````
```python
Expand All @@ -88,9 +88,9 @@ async def handler(request):
```
````

默认情况下,[`ujson`](https://github.com/ultrajson/ultrajson)的 Sanic 船舶,作为其JSON 编码器。 如果未安装 `ujson` ,它将返回到标准库`json` 模块。
默认情况下,Sanic 使用 [`ujson`](https://github.com/ultrajson/ultrajson) 作为其首选的 JSON 解析器。 如果`ujson`模块没有被安装,程序将会退回到使用标准库中的`json`模块。

如果你想要更改这一点是非常简单的。
想要改变默认的json解析器,非常简单

```python
from sanic import json
Expand All @@ -99,36 +99,36 @@ from orjson import dumps
json({"foo": "bar"}, dumps=dumps)
```

您还可以声明在初始化时在全局使用哪些实现:
您还可以在初始化时全局声明在整个应用程序中使用哪个json解析器

```python
orjson 导入转储
from orjson import dumps

应用 = Sanic(..., dumps=dumps)
app = Sanic(..., dumps=dumps)
```

### 文件
### File(文件)

.. :
.. column::

```
**默认内容类型**:N/
**默认Content-Type**:N/A
**描述**:返回一个文件
```

.. :
.. column::

````
```python
from sanic import file
@app.route("/")
async def handler(request):
return 等待文件 ("/path/to/whatever.png")
return await file("/path/to/whatever.png")
```
````

Sanic 将检查该文件,并尝试和猜其mime 类型并使用一个适当的内容类型值。 如果您想要
Sanic 将会检查该文件,并尝试猜测其 MIME 类型,然后为内容类型使用合适的值。 如果您愿意,也可以明确指定

```python
file("/path/to/whatever.png", mime_type="image/png")
Expand All @@ -137,19 +137,19 @@ file("/path/to/whatever.png", mime_type="image/png")
您也可以选择覆盖文件名称:

```python
file("/path/to/whatever.png", filename="超级超棒不可思议的.png")
file("/path/to/whatever.png", filename="super-awesome-incredible.png")
```

### 文件流
### File Streaming(文件流

.. :
.. column::

```
**默认内容类型**: N/A
**默认Content-Type**: N/A
**描述**: 流一个文件到一个客户端, 当像视频一样流出大文件时有用。
```

.. :
.. column::

````
```python
Expand All @@ -161,105 +161,104 @@ async def handler(request):
```
````

`file()`方法一样`file_stream()`会尝试确定文件的 mime 类型。
`file()` 方法类似`file_stream()` 也会尝试确定文件的 MIME 类型。

### 原始文件
### Raw(原始数据)

.. :
.. column::

```
**默认Content-Type**: `application/octet-stream`
**Description**: 发送原始字节而不对正文进行编码
**描述**: 发送原始字节而不对正文进行编码
```

.. :
.. column::

````
```python
from sanic import raw
@app.route("/")
async def handler(request):
return raw(B"raw bytes")
return raw(b"raw bytes")
```
````

### 重定向
### Redirect(重定向

.. :
.. column::

```
**默认Content-Type**: `text/html; charset=utf-8`
**Description**: 发送一个 `302` 响应来将客户重定向到另一个路径
**描述**: 发送一个 `302` 响应来将客户重定向到另一个URL
```

.. :
.. column::

````
```python
from sanic import redirecte
from sanic import redirect
@app.route("/")
async def handler(request):
return redirect("/login")
```
````

### 空的
### Empty(空返回)

.. :
.. column::

```
**默认 Content-Type**: N/A
**Description**: 为响应[RFC 2616](https://tools.ietf.org/search/rfc2616#section-7.2.1)
**默认 Content-Type**: N/A
**描述**: 对于按照 [RFC 2616](https://tools.ietf.org/search/rfc2616#section-7.2.1) 规定响应空消息
```

.. :
.. column::

````
```python
来自sanic import
from sanic import empty
@app.route("/")
async def handler(request):
return empty()
```
默认`204` 状态。
默认的状态码是 `204`
````

## 默认状态
## Default Status(默认状态码)

响应默认的 HTTP 状态代码是 \`200'。 如果您需要更改它,它可以通过响应方式完成
响应默认的 HTTP 状态代码是 `200'。 如果您需要更改它,它可以通过在响应函数中传入指定的`status\`

```python
@app.post("/")
async def create_new(request):
new_thing = 等待do_create(request)
new_thing = await do_create(request)
return json({"created": True, "id": new_thing.thing_id}, status=201)
```

## 返回 JSON 数据
## Returning JSON data(返回json数据)

Starting in v22.12, When you use the `sanic.json` convenience method, it will return a subclass of `HTTPResponse` called :class:`sanic.response.types.JSONResponse`. This object will
have several convenient methods available to modify common JSON body.
从 v22.12 版本开始,当您使用 `sanic.json` 的便捷方法时,它将返回一个名为 :class:`sanic.response.types.JSONResponse``HTTPResponse` 子类。 此对象将提供多个便捷方法来修改常见的 JSON 正文。

```python
从沙尼导入 json
from sanic import json

resp = json(...)
```

- `resp.set_body(<raw_body>)` - 设定JSON对象的正文到传递的值
- `resp.append(<value>)` - 追加一个 `list.append` 这个物体的值(仅当root JSON 是一个数组时才起作用)
- `resp.extend(<value>)` - 将一个值扩展到物体像`list.extend` (仅当root JSON 是一个数组时才能工作)
- `resp.update(<value>)` - 更新像`dict.update` 这样的物体(仅当root JSON是一个对象时才工作)
- `resp.pop()` - 弹出一个 `list.pop``dict.pop` 等值(仅当root JSON 是数组或对象时才起作用)
- `resp.set_body(<raw_body>)` - 将 JSON 对象的正文设置为传递的值
- `resp.append(<value>)` - 向正文追加一个值,如同 `list.append`(仅当根 JSON 是数组时有效)
- `resp.extend(<value>)` - 将一个值扩展到正文中,如同 `list.extend`(仅当根 JSON 是数组时有效)
- `resp.update(<value>)` -使用类似 `dict.update` 的方式更新正文(仅当根 JSON 是对象时有效)
- `resp.pop()` - 移除并返回一个值,如同 `list.pop``dict.pop`(仅当根 JSON 是数组或对象时有效)

.. 警告::
.. warning:: 警告⚠

```
原生的 Python 对象作为`raw_body` 存储在 `JSONResponse` 对象上。 虽然用新值覆盖此值是安全的,但是你应该**不应该**试图变换它。 你应该使用上面列出的方法
原始 Python 对象作为 `raw_body` 存储在 `JSONResponse` 对象上。虽然您可以安全地用新值覆盖这个值,但您应该**不要**尝试对其进行修改。相反,您应该使用上面列出的方法进行操作
```

```python
Expand All @@ -279,20 +278,20 @@ resp.raw_body.update({"something": "else"})
```

```python
# 或者,甚至将其视为列表
# Or, even treat it like a list
resp = json(["foo", "bar"])

# 这是非常重要的
resp. aw_body = ["foo", "bar", "something", "else"]
# This is OKAY
resp.raw_body = ["foo", "bar", "something", "else"]

# 这是更好的
resp. xtend(["something", "else"])
# This is better
resp.extend(["something", "else"])

# 这也很适合
# This is also works well
resp.append("something")
resp.append("else")

# 这不是很重要的
# This is NOT OKAY
resp.raw_body.append("something")
```

Expand Down

0 comments on commit 6fee283

Please sign in to comment.