This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sanic-org/Simplified-Chinese
Simplified chinese
- Loading branch information
Showing
54 changed files
with
4,403 additions
and
211 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 高级用法(Advanced) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
::: |
Oops, something went wrong.