django rest framework中文介绍

image.png

注意:这是版本3的文档。还提供了版本2的文档。


image.png

Django REST framework 是一个强大且灵活的工具包,用以构建Web APIs。
为什么要使用REST framework?

Funding(资金来源)

REST framework is a collaboratively(合作地) funded project(基金项目). If you use REST framework commercially(商业化的) we strongly(强烈) encourage(建议) you to invest(投资) in its continued development(可持续发展) by signing up for a paid plan.(注册付费计划)

Every single(每次简单) sign-up helps us make REST framework long-term financially(财政上) sustainable(财务上可持续发展)

Many thanks to all our wonderful sponsors(赞助商), and in particular to our premium backers(优质的支持者), Rover, Sentry, Stream, Machinalis, and Rollbar.
(非常感谢我们所有的优秀赞助商,特别是我们的优秀支持者, Rover, Sentry, Stream, Machinalis, and Rollbar.

image.png

配置要求( Requirements

REST framework 有以下的要求:

Python (2.7, 3.2, 3.3, 3.4, 3.5,3.6)
Django (1.7+, 1.8, 1.9,,2.0)

下面是可选的包:

安装 Installation

Install using pip, including any optional packages you want...(使用pip安装,包括任何你想要的可选包裹...)

pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support

...or clone the project from github.(或者从GitHub复制项目)

git clone git@github.com:encode/django-rest-framework.git

Add 'rest_framework' to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'rest_framework',
)

If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.(如果您打算使用可浏览的API,您可能还需要添加REST框架的登录和注销视图。将以下内容添加到您的根urls.py文件中。)

urlpatterns = [
    ...
    url(r'^api-auth/', include('rest_framework.urls'))
]

Note that the URL path can be whatever you want.(注意,url路径可以是任何你想要的。)

示例 Example

Let's take a look at a quick example of using REST framework to build a simple model-backed API.让我们来看看一个使用REST framework构建一个简单模型支持api的快速示例。

We'll create a read-write API for accessing information on the users of our project.(我们将创建一个读写api,用于访问项目用户的信息。)

Any global settings for a REST framework API are kept in a single configuration dictionary named REST_FRAMEWORK. Start off by adding the following to your settings.py module:(REST framework api的任何全局设置都保存在一个名为“rest_wramework”的配置词典中。首先在“环境”模块中添加以下内容:)

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

Don't forget to make sure you've also added rest_framework to your INSTALLED_APPS.(别忘了确保你已经在“INSTALLED_APPS”中添加了“rest_framework”。)

We're ready to create our API now. Here's our project's root urls.py module:(我们准备好创建我们的api了。这是我们项目的根源 urls.py模块:)

from django.conf.urls import url, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')

# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

You can now open the API in your browser at http://127.0.0.1:8000/, and view your new 'users' API. If you use the login control in the top right corner you'll also be able to add, create and delete users from the system.(现在,您可以在浏览器中输入‘http://127.0.0.1:8000/’打开api,并查看您的新“用户”api。如果您使用右上角的登录控件,您也可以从系统中添加、创建和删除用户。)

快速启动 Quickstart

Can't wait to get started? The quickstart guide is the fastest way to get up and running, and building APIs with REST framework.(等不及要开始了?快速启动指南是最快的建立和运行的方式,并建立REST framework的apis。)

教程 Tutorial

The tutorial will walk you through the building blocks that make up REST framework. It'll take a little while to get through, but it'll give you a comprehensive understanding of how everything fits together, and is highly recommended reading.(本教程将帮助您完成组成REST框架的构建块。这需要一点时间来完成,但是它会给你一个全面的理解如何把一切结合起来,并强烈推荐阅读。)

There is a live example API of the finished tutorial API for testing purposes, available here.这里有一个用于测试目的的完成教程API的实例化API,这里可获得

API指南 API Guide

The API guide is your complete reference manual to all the functionality provided by REST framework.(API指南是您对REST框架提供的所有功能的完整参考手册)

论题Topics

General guides to using REST framework.(使用REST框架的一般指南。)

发展 Development

See the Contribution guidelines for information on how to clone the repository, run the test suite and contribute changes back to REST Framework.(有关如何克隆存储库、运行测试套件以及向REST框架贡献更改的信息,请参阅贡献指南。)

支持Support

For support please see the REST framework discussion group, try the #restframework channel on irc.freenode.net, search the IRC archives, or raise a question on Stack Overflow, making sure to include the 'django-rest-framework' tag.(要获得支持,请参阅REST框架讨论组,在IRC .freenode.net上尝试#restframework通道,搜索IRC档案,或者对Stack Overflow提出问题,确保包含“django-rest-framework”标签。)

For priority support please sign up for a professional or premium sponsorship plan.(如需优先支持,请注册专业或优质赞助计划。)

For updates on REST framework development, you may also want to follow the author on Twitter.(对于REST框架开发的更新,您可能还希望在Twitter上跟随作者。)

Follow @_tomchristie

安全性 Security

If you believe you’ve found something in Django REST framework which has security implications, please do not raise the issue in a public forum.

Send a description of the issue via email to rest-framework-security@googlegroups.com. The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.

许可证 License

Copyright (c) 2011-2017, Tom Christie All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,109评论 0 10
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,306评论 5 6
  • 很多人都有情人,而且不止一个。不同时期,或者同时拥有很多个。可是长久的却不多。为什么?有人说,玩够了,和爱人一样,...
    一休茶馆阅读 2,844评论 0 0
  • 唤醒49-30 看到 看到行为背后的正面动机 真心英雄在爱中自由的戒律之一就是相信每个人每个行为背后...
    我和榕树阅读 160评论 0 2
  • 泡在水里太久了 我都忘记了太阳的脸 是什么让老天流了那么多 那么久的泪 是我们的无知 还是人间的诸多灾难 连日来全...
    漂哥阅读 313评论 1 7