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閱讀 229,565評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,398評論 0 10
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,512評論 5 6
  • 很多人都有情人,而且不止一個。不同時期,或者同時擁有很多個。可是長久的卻不多。為什么?有人說,玩夠了,和愛人一樣,...
    一休茶館閱讀 3,006評論 0 0
  • 喚醒49-30 看到 看到行為背后的正面動機 真心英雄在愛中自由的戒律之一就是相信每個人每個行為背后...
    我和榕樹閱讀 170評論 0 2
  • 泡在水里太久了 我都忘記了太陽的臉 是什么讓老天流了那么多 那么久的淚 是我們的無知 還是人間的諸多災難 連日來全...
    漂哥閱讀 337評論 1 7