Django REST framework的系列教程
对于需要通篇了解的同学,可以点击教程索引目录。
from rest_framework.views import APIView
class all_classroom(APIView):
def get(self,request):
classroom = Classroom.objects.all()
ser = serailizers_classroom(classroom,many=True)
return Response(ser.data,status=200)
class single_classroom(APIView):
def get(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
ser = serailizers_classroom(classroom)
return Response(ser.data,status=200)
def put(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
ser = serailizers_classroom(classroom,data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data,status=200)
return Response(ser.errors)
def delete(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
classroom.delete()
return Response(status=200)
## 主url设置
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## 子url设置
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view()),
url('^classroom/(?P\d+)/$' , single_classroom.as_view())
]
from rest_framework.generics import GenericAPIView
from rest_framework import mixins
class all_classroom(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
def get(self,request):
return self.list(request)
def post(self,request):
return self.create(request)
class single_classroom(GenericAPIView,mixins.RetrieveModelMixin,mixins.DestroyModelMixin,mixins.UpdateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
def get(self,request,pk):
return self.retrieve(request)
def put(self,request,pk):
return self.update(request)
def delete(self,request,pk):
return self.destroy(request)
## 主url设置
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## 子url设置
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view()),
url('^classroom/(?P\d+)/$' , single_classroom.as_view())
]
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
class all_classroom(GenericViewSet,ListModelMixin,CreateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
class single_classroom(GenericViewSet,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
## 主url设置
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## 子url设置
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view({'get':'list','post':'create'})),
url('^classroom/(?P\d+)$' , single_classroom.as_view({'get':'retrieve','put':'update','delete':'destroy'}))
]
from rest_framework.viewsets import GenericViewSet
from rest_framework.generics import CreateAPIView,ListAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView
class all_classroom(GenericViewSet,CreateAPIView,ListAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
## 主url设置
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## 子url设置
from django.views.generic import TemplateView
from rest_framework.routers import DefaultRouter
from day1106_app.views import all_classroom, all_students
rout = DefaultRouter()
rout.register('classroom',all_classroom)
rout.register('students',all_students)
urlpatterns = [
url('^$', TemplateView.as_view(template_name='index.html')),
]
urlpatterns += rout.urls
from rest_framework.viewsets import ModelViewSet
class all_classroom(ModelViewSet):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
class all_students(ModelViewSet):
queryset = Students.objects.all()
serializer_class = serailizers_students
## 主url设置
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## 子url设置
from django.views.generic import TemplateView
from rest_framework.routers import DefaultRouter
from day1106_app.views import all_classroom, all_students
rout = DefaultRouter()
rout.register('classroom',all_classroom)
rout.register('students',all_students)
urlpatterns = [
url('^$', TemplateView.as_view(template_name='index.html')),
]
urlpatterns += rout.urls