restful
一個url路由 對應一個功能,操作,絕大多數的操作都是針對資料庫的增刪改查
前後端分離,都是ajax axios,所有的介面都是接收json,返回json
Api 介面是提供給程式訪問的 ,提供了許可權和限流認證
restful 攜帶token 需要在請求頭中 用鍵值對的方式攜帶token
可以通過restful-jwt 提供的認證檢視來處理 使用者認證
在 app.urls中配置路由
from rest_framework_jwt.views import obtain_jwt_token
path('login/', obtain_jwt_token)
在settings.py中配置 許可權認證配置
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES': {
'user': '10/hour',
'anon': '5/day'
},
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_jwt.authentication.JSONWebTokenAuthentication'
]
}
JWT_AUTH = {
'JWT_PAYLOAD_HANDLER': 'rest_framework_jwt.utils.jwt_payload_handler',
'JWT_AUTH_HEADER_PREFIX': 'JWT',
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_ALLOW_REFRESH': True,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(hours=24)
}
permission_classes = (AllowAny,)
permission_classes = (IsAuthenticated,)
throttle_classes = [AnonRateThrottle]
在settings.py中配置限流次數
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES':{
'user':'10/hour',
'anon':'5/day'
}
}
def book_view(request):
if request.method == 'GET':
books = BookInfo.objects.all()
book_list = []
for book in books:
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
book_list.append(book_data)
return JsonResponse(book_list, safe=False)
if request.method == 'POST':
btitle = request.POST.get('btitle')
bpub_date = request.POST.get('bpub_date')
bread = request.POST.get('bread')
bcomment = request.POST.get('bcomment')
if all([btitle, bpub_date]):
pass
else:
return JsonResponse({'code': 400, 'msg': '缺少引數'})
book = BookInfo.objects.create(btitle=btitle, bpub_date=bpub_date)
book.save()
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
return JsonResponse(book_data)
class BookView(View):
def get(self, request):
books = BookInfo.objects.all()
book_list = []
for book in books:
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
book_list.append(book_data)
return JsonResponse(book_list, safe=False)
def post(self, request):
data = json.loads(request.body.decode())
btitle = data.get('btitle')
bpub_date = data.get('bpub_date')
if all([btitle, bpub_date]):
pass
else:
return JsonResponse({'code': 400, 'msg': '缺少引數'})
book = BookInfo.objects.create(btitle=btitle, bpub_date=bpub_date)
book.save()
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
return JsonResponse(book_data)
class BookAPIView(APIView):
permission_classes = (IsAuthenticated,)
throttle_classes = [AnonRateThrottle]
def get(self, request):
books = BookInfo.objects.all()
book_list = []
for book in books:
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
book_list.append(book_data)
return Response(book_list)
def post(self, request):
btitle = request.data.get('btitle')
bpub_date = request.data.get('bpub_date')
if all([btitle, bpub_date]):
pass
else:
return Response({'code': 400, 'msg': '缺少引數'})
book = BookInfo.objects.create(btitle=btitle, bpub_date=bpub_date)
book.save()
book_data = {
'id': book.id,
'btitle': book.btitle,
'bpub_date': book.bpub_date,
'bread': book.bread,
'bcomment': book.bcomment
}
return Response(book_data)