Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Simplified chinese #12

Merged
merged 24 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f14e6c8
add chinese support
miss85246 Jan 24, 2021
6003b49
Merge branch 'master' of github.com:miss85246/sanic-guide
miss85246 Jan 24, 2021
104a3bf
fixed spell error: endpoing -> endpoint
miss85246 Jan 24, 2021
06d795a
更新部分中文文档
miss85246 Jan 25, 2021
bc42255
Merge branch 'master' of github.com:miss85246/sanic-guide
miss85246 Jan 25, 2021
4f84ff0
Update routing.md
miss85246 Jan 25, 2021
8cdf3f7
translate listener
miss85246 Jan 25, 2021
2c8280c
complated translation of cookies.md and polish app.md
miss85246 Jan 26, 2021
a38eb27
Complete translation of class-based-views.md tasks.md
miss85246 Jan 28, 2021
330f278
Update middleware.md
miss85246 Feb 2, 2021
e0a337b
Completed translation of middleware.md
miss85246 Feb 2, 2021
eaada2e
Merged Chinese translation repo
miss85246 Feb 2, 2021
8c8194e
add simplified chinese translation for proxy configuration.md
ZinkLu Feb 2, 2021
01a569c
add simplified chinese translation for streaming.md
ZinkLu Feb 2, 2021
42858ff
Merge pull request #2 from sanic-org/Simplified-Chinese
miss85246 Feb 4, 2021
72f3e19
Update configuration.md
miss85246 Feb 5, 2021
51e9a7d
Complete the translation of the deployment chapter
miss85246 Feb 7, 2021
b85005b
finish advanced part
ZinkLu Feb 15, 2021
db7c1f7
Complete the translation of logging
miss85246 Feb 23, 2021
52ca62f
Merge pull request #3 from sanic-org/Simplified-Chinese
miss85246 Feb 26, 2021
dfa0dbd
Complete the translation of exceptions
miss85246 Feb 26, 2021
71cd093
Merge pull request #10 from miss85246/Simplified-Chinese
miss85246 Feb 26, 2021
112be6b
add best-practices/buleprint part
ZinkLu Feb 28, 2021
ae92cdf
Merge branch 'master' into Simplified-Chinese
ZinkLu Mar 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
550 changes: 388 additions & 162 deletions src/.vuepress/config.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/guide/basics/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def halt_response(request, response):
```
:---

## Order of execution
#### Order of execution

Request middleware is executed in the order declared. Response middleware is executed in **reverse order**.

Expand Down
2 changes: 1 addition & 1 deletion src/guide/basics/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ But what is it? And how do we use it?

---:1

The most basic way to wire up a handler to an endpoing is with `app.add_route()`.
The most basic way to wire up a handler to an endpoint is with `app.add_route()`.

See [API docs]() for more details.
:--:1
Expand Down
2 changes: 1 addition & 1 deletion src/guide/deployment/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if __name__ == "__main__":
```

::: warning
Sanic's debug more will slow down the server's performance and is therefore advised to enable it only in development environments.
Sanic's debug mode will slow down the server's performance and is therefore advised to enable it only in development environments.
:::
## Automatic Reloader

Expand Down
25 changes: 25 additions & 0 deletions src/zh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
home: true
heroImage: https://raw.githubusercontent.com/huge-success/sanic-assets/master/png/sanic-framework-logo-400x97.png
heroText: Build fast. Run Fast.
tagline: 异步 Python 3.6+ web 框架
actionText: 快速开始 →
actionLink: /zh/guide/
features:
- title: 简单轻便
details: 开箱即用,您可以快速构建属于您的 API 应用程序
- title: 灵巧无束
details: 按照您的意愿进行自由创建,不会对您造成任何约束
- title: 易于拓展
details: 具备优秀的拓展性,随时可以为各种大小的 Web 应用程序提供支持。
- title: 自给自足
details: Sanic 不仅是一个框架,也是一个服务器,它可以随时为您编写的 Web 应用程序提供部署服务。
- title: 备受信赖
details: Sanic 是 PyPI 上最受欢迎的整体框架之一,是顶级的异步 Web 框架
- title: 社区驱动
details: 该项目由社区为社区维护和运行,具有大量的活跃贡献者。
footer: MIT Licensed | Copyright © 2018-present Sanic Community Organization
pageClass: landing-page
logo: false
---

87 changes: 87 additions & 0 deletions src/zh/guide/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
pageClass: intro
---

# 介绍

::: warning 🚧正在施工🚧

这些文件仍在建设中。它最终将会取代 readthedocs 上的文档。另外该文档将是严格的API文档。欢迎在 [Github仓库](https://github.com/sanic-org/sanic-guide) 或 [论坛](https://community.sanicframework.org/t/frontpage-and-documentation-overhaul/770) 上提出任何建议 或 提供任何支持。

最终版本预计将与21.3版本一致。

*更新于 2021 年 1 月*
:::

Sanic 是 Python 3.6+ Web 服务器和 Web 框架,旨在提高性能。它允许使用 Python 3.5 中添加的 `async`/`await`等待语法,这使得您的代码有效的避免阻塞从而达到提升响应速度的目的。

| | |
|---------|-------------------------------------------------------------------------------------------------------------------------|
| Build | [![Build Status][]][1] [![AppVeyor Build Status][]][2] [![Codecov]][3] |
| Docs | [![Documentation]][4] |
| Package | [![PyPI][]][5] [![PyPI version][]][5] [![PyPI Wheel][]][6] [![Supported implementations][]][6] [![Code style black]][7] |
| Support | [![Forums][]][8] [![Join the chat at <https://gitter.im/sanic-python/Lobby>][]][9] [![Awesome Sanic List]][10] |
| Stats | [![Downloads][]][11] [![Downloads][12]][11] |

## 它是什么?

首先,在入坑之前, 你应该知道 Sanic 框架和其他的框架相比是与众不同的。

哦,纠正一下,就在上面的那一句中,隐藏了一个巨大的错误,因为 Sanic 不仅仅是一个 **框架**,它还是一个 **Web 服务器**, 在后面的 **部署** 环节中,我们将仔细地探讨这个问题。

但是,请记住,Sanic具备开箱即用的功能,它可以用于编写,部署和扩展生产级Web应用程序​ :rocket:。

## 目标

> 提供一种简单且快速,集创建和启动于一体的方法,来实现一个易于修改和拓展的 HTTP 服务
## 特征

---:1

- 内置极速 web server
- 生产准备就绪
- 极高的拓展性
- 支持 ASGI
- 简单直观的 API 设计
- 社区保障
:--:1

:---



## 加入社区

Sanic 的主要讨论渠道是通过 [社区论坛](https://community.sanicframework.org/) 当然也有 [Gitter 频道](https://gitter.im/sanic-python/Lobby) 但是我们更喜欢使用社区论坛,因为这样可以使我们更方便在未来管理历史讨论记录。

项目维护人员正在积极监视 Stackoverflow `[sanic]标签`, 点此 [快速访问](https://stackoverflow.com/questions/tagged/sanic)

## 贡献

我们总是很高兴有新的贡献。 我们已经为那些希望入门的人提供了 [标记好的问题](https://github.com/sanic-org/sanic/issues?q=is%3Aopen+is%3Aissue+label%3Abeginner),并欢迎您在[论坛上的问题/解答/讨论](https://community.sanicframework.org/)。 请查看我们的[贡献准则](https://sanic.readthedocs.io/en/latest/sanic/contributing.html)。

[Build Status]: https://travis-ci.com/sanic-org/sanic.svg?branch=master
[1]: https://travis-ci.com/sanic-org/sanic
[AppVeyor Build Status]: https://ci.appveyor.com/api/projects/status/d8pt3ids0ynexi8c/branch/master?svg=true
[2]: https://ci.appveyor.com/project/sanic-org/sanic
[Codecov]: https://codecov.io/gh/sanic-org/sanic/branch/master/graph/badge.svg
[3]: https://codecov.io/gh/sanic-org/sanic
[Documentation]: https://readthedocs.org/projects/sanic/badge/?version=latest
[4]: http://sanic.readthedocs.io/en/latest/?badge=latest
[PyPI]: https://img.shields.io/pypi/v/sanic.svg
[5]: https://pypi.python.org/pypi/sanic/
[PyPI version]: https://img.shields.io/pypi/pyversions/sanic.svg
[PyPI Wheel]: https://img.shields.io/pypi/wheel/sanic.svg
[6]: https://pypi.python.org/pypi/sanic
[Supported implementations]: https://img.shields.io/pypi/implementation/sanic.svg
[Code style black]: https://img.shields.io/badge/code%20style-black-000000.svg
[7]: https://github.com/ambv/black
[Forums]: https://img.shields.io/badge/forums-community-ff0068.svg
[8]: https://community.sanicframework.org/
[Join the chat at <https://gitter.im/sanic-python/Lobby>]: https://badges.gitter.im/sanic-python/Lobby.svg
[9]: https://gitter.im/sanic-python/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
[Awesome Sanic List]: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
[10]: https://github.com/mekicha/awesome-sanic
[Downloads]: https://pepy.tech/badge/sanic/month
[11]: https://pepy.tech/project/sanic
[12]: https://pepy.tech/badge/sanic/week
1 change: 1 addition & 0 deletions src/zh/guide/advanced/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 高级用法(Advanced)
230 changes: 230 additions & 0 deletions src/zh/guide/advanced/class-based-views.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
# 基于类的视图(Class Based Views)

## 为什么要使用它?(Why use them?)

---:1

### 困境(The problem)

在日常的 API 设计过程中,将不同的响应函数通过不同的 HTTP 方法挂载到同一路由上是一种常用的设计模式。

虽然我们之前讲到的方式也能够实现同样的效果,但事实证明它们并不是一种好的设计实践。随着时间的推移以及项目的发展,它们将变得越来越难以维护:

:--:1

```python
@app.get("/foo")
async def foo_get(request):
...

@app.post("/foo")
async def foo_post(request):
...

@app.put("/foo")
async def foo_put(request):
...

@app.route("/bar", methods=["GET", "POST", "PATCH"])
async def bar(request):
if request.method == "GET":
...
elif request.method == "POST":
...
elif request.method == "PATCH":
...
```
:---

---:1

### 破局(The solution)

基于类的视图是一种实现了响应请求行为的类, 该类提供了一种在同一路由上分隔处理不同HTTP请求类型的方法。

:--:1

```python
from sanic.views import HTTPMethodView

class FooBar(HTTPMethodView):
async def get(self, request):
...

async def post(self, request):
...

async def put(self, request):
...

app.add_route(FooBar.as_view(), "/foobar)
```
:---

## 定义视图(Defining a view)

基于类的视图应该是 `HTTPMethodView` 的子类 。您可以使用相应的 HTTP 方法的名称实现类方法。如果收到的请求没有定义的方法,将生成 `405: Method not allowed` 响应。

---:1

若想要将基于类的视图挂载到路由上,则应该使用 `app.add_route` 方法,其中第一个参数是使用 `as_view` 调用后的已经定义好的类,第二个参数是要分配的 URL 路由。

HTTPMethodView 支持的方法有:

- get
- post
- put
- patch
- delete
- head
- options

:--:1

```python
from sanic.views import HTTPMethodView
from sanic.response import text

class SimpleView(HTTPMethodView):

def get(self, request):
return text("I am get method")

# You can also use async syntax
async def post(self, request):
return text("I am post method")

def put(self, request):
return text("I am put method")

def patch(self, request):
return text("I am patch method")

def delete(self, request):
return text("I am delete method")

app.add_route(SimpleView.as_view(), "/")
```
:---

## 路由参数(Path parameters)

---:1

您完全可以按照我们在 [路由](/zh/guide/basics/routing.md) 这一章节所讨论的使用方式来使用路由参数。

:--:1

```python
class NameView(HTTPMethodView):

def get(self, request, name):
return text("Hello {}".format(name))

app.add_route(NameView.as_view(), "/<name>")
```
:---

## 装饰器(Decorators)

就像 [装饰器](/zh/guide/best-practices/decorators.md) 这一章节所述,您可能经常需要使用装饰器来对您的响应程序添加额外的功能,基于类的视图给出了两种方式来添加装饰器:

1. 应用于视图中的 *所有* HTTP 方法
2. 独自应用于视图中的 *指定* HTTP 方法

---:1

### 用于所有方法(Apply to all methods)

如果您想要添加应用于所有方法的类,您可以通过类变量 `decorators` 来实现,设置后,这些装饰器将在调用 `as_view` 时应用于类。

:--:1

```python
class ViewWithDecorator(HTTPMethodView):
decorators = [some_decorator_here]

def get(self, request, name):
return text("Hello I have a decorator")

def post(self, request, name):
return text("Hello I also have a decorator")

app.add_route(ViewWithDecorator.as_view(), "/url")
```
:---

---:1

### 应用于单个方法(Apply to individual methods)

但是,如果您只是想装饰一些方法,而不是所有的方法,你可以这样使用:

:--:1

```python
class ViewWithSomeDecorator(HTTPMethodView):

@staticmethod
@some_decorator_here
def get(request, name):
return text("Hello I have a decorator")

def post(self, request, name):
return text("Hello I don"t have any decorators")

@some_decorator_here
def patch(self, request, name):
return text("Hello I have a decorator")
```
:---

## 添加路由(Generating a URL)

---:1

和路由章节中的 [添加路由](/zh/guide/basics/routing.md#generating-a-url) 一样,除了指定需要添加的类之外,其他的使用方法是一致的。

:--:1

```python
@app.route("/")
def index(request):
url = app.url_for("SpecialClassView")
return redirect(url)


class SpecialClassView(HTTPMethodView):
def get(self, request):
return text("Hello from the Special Class View!")


app.add_route(SpecialClassView.as_view(), "/special_class_view")
```
:---

## 合成视图(Composition view)

作为 `HTTPMethodView` 的替代方法,可以使用 `CompositionView` 将处理程序函数移至视图类之外。

每个支持的 HTTP 方法的响应程序都在源代码的其他地方定义,然后使用 `CompositionView.add` 方法添加到视图中来。 第一个参数是要处理的 HTTP 方法的列表(例如 `[“ GET”,“ POST”]` ),第二个参数是处理函数。

```python
from sanic.views import CompositionView

def get_handler(request):
return text("I am a get method")

view = CompositionView()
view.add(["GET"], get_handler)
view.add(["POST", "PUT"], lambda request: text("I am a post/put method"))

# Use the new view to handle requests to the base URL
app.add_route(view, "/")
```

::: warning

目前为止,Sanic 暂时不支持您使用 `url_for` 来为 `CompositionView` 生成 URL

:::
Loading