Commit 23d86193 authored by Administrator's avatar Administrator
Browse files

added custom permission; added login/logout function

parent f36e7f0f
...@@ -21,5 +21,6 @@ from rest_framework.documentation import include_docs_urls ...@@ -21,5 +21,6 @@ from rest_framework.documentation import include_docs_urls
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('my_app/', include('my_app.urls')), path('my_app/', include('my_app.urls')),
path('docs/', include_docs_urls(title="DRF API文档", description="Django REST Framework快速入门")) path('docs/', include_docs_urls(title="DRF API文档", description="Django REST Framework快速入门")),
path('api-auth', include('rest_framework.urls'))
] ]
...@@ -14,12 +14,17 @@ from rest_framework.views import APIView ...@@ -14,12 +14,17 @@ from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
# Import for custom permission
from my_app.permissions import CustomPermission
""" """
CBV - Class based views CBV - Class based views
""" """
# Used to query multiple items or create new item # Used to query multiple items or create new item
class DemoUserList(APIView): class DemoUserList(APIView):
permission_classes = [CustomPermission]
# Query all items # Query all items
def get(self, request, format=None): def get(self, request, format=None):
demo_users = DemoUsers.objects.all() demo_users = DemoUsers.objects.all()
...@@ -28,6 +33,7 @@ class DemoUserList(APIView): ...@@ -28,6 +33,7 @@ class DemoUserList(APIView):
# Create new item # Create new item
def post(self, request, format=None): def post(self, request, format=None):
serializer = DemoUsersSerializer(data=request.data) serializer = DemoUsersSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
serializer.save(created_by=self.request.user.username) serializer.save(created_by=self.request.user.username)
...@@ -42,6 +48,8 @@ class DemoUserList(APIView): ...@@ -42,6 +48,8 @@ class DemoUserList(APIView):
# Used to query/update/delete single item # Used to query/update/delete single item
class DemoUserDetail(APIView): class DemoUserDetail(APIView):
permission_classes = [IsAuthenticated]
# Implementation of get single item # Implementation of get single item
def get_object(self, pk): def get_object(self, pk):
try: try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment