官方文檔
https://www.django-rest-framework.org/#
安裝
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
配置
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.
urlpatterns = [
...
path('', include('rest_framework.urls'))
# url(r'^api-auth/', include('rest_framework.urls'))
]
例子
REST框架API的任何全局設置都保存在一個名爲REST_FRAMEWORK的配置字典中。首先在settings.py模塊中添加以下內容:
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'
]
}
現在我們已經準備好創建API了。這是我們項目的根url .py模塊:
在這裏插入代碼片