一、使用Pip安裝Django REST Framework:python
pip install djangorestframework
二、在Setting中配置 INSTALLED_APPS
:
數據庫
三、最後同步數據庫django
APIView繼承自View,並對其進行了包裝成高階Request。Request中傳入了更多參數,如權限認證。函數
我接着在API View 執行initial函數時,會對request進行驗證,確保來訪的請求是被容許的。學習
咱們能夠經過繼承APIView,重寫其 authentication_classes 屬性,來自定義認證器,如驗證請求中是否含有token字段!spa
from rest_framework.views import APIView
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import NotAuthenticated
class MyUser(object):
is_authenticated = True
class MyAuthentication(object):
def authenticate(self, request):
token = request._request.GET.get('token')
if not token:
raise NotAuthenticated()
return (MyUser,None)
def authenticate_header(self, val):
pass
API3d