1、引入rest_framework.authtoken
setting.py
INSTALLED_APPS = [
......
# Third party apps
'rest_framework',
'rest_framework.authtoken',
......
]
2、执行migrate
会在数据库中建表authtoken_token
3、采用CBV模式建立rest服务
url.py
from sysmanage.views import AuthViewSet
urlpatterns = [
url(r'^auth', AuthViewSet.as_view())
]
view.py
from django.views import View
class AuthViewSet(View):
def get(self, request, *args, **kwargs):
result = {
'status': True,
'data': 'response data'
}
return JsonResponse(result, status=200)
def post(self, request, *args, **kwargs):
result = {
'status': True,
'data': 'response data'
}
return JsonResponse(result, status=200)