實作
修改 requirements.txt 檔,添加依賴項目。
| 1
 | djangorestframework-simplejwt
 | 
安裝依賴套件。
| 1
 | pip install -r requirements.txt
 | 
修改 example/settings.py 檔。
| 12
 3
 4
 5
 
 | REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': (
 'rest_framework_simplejwt.authentication.JWTAuthentication',
 ),
 }
 
 | 
修改 example/urls.py 檔。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | from rest_framework_simplejwt.views import (
 TokenObtainPairView,
 TokenRefreshView,
 TokenVerifyView,
 )
 
 urlpatterns = [
 
 path('api/token', TokenObtainPairView.as_view(), name='obtain_token'),
 path('api/token/refresh', TokenRefreshView.as_view(), name='refresh_token'),
 path('api/token/verify', TokenVerifyView.as_view(), name='verify_token'),
 ]
 
 | 
修改 articles/views.py 檔。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | from rest_framework.permissions import IsAuthenticated
 
 
 class ArticleViewSet(viewsets.ModelViewSet):
 permission_classes = [IsAuthenticated]
 serializer_class = ArticleSerializer
 queryset = Article.objects.all()
 
 
 
 | 
建立使用者。
| 1
 | python manage.py createsuperuser
 | 
參考資料