路由
1、路由第一种写法-原始方式
这种方式也就是前面一直在写的;
urls.py
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/$', views.BookView.as_view()), url(r'^books/(?P<pk>d+)$', views.BookDetailView.as_view()), ]
views.py
class BookView(APIView): def get(self, request): book_list = models.Book.objects.all() bs = BookSerializers(book_list, many=True) return Response(bs.data) def post(self, request): # 添加一条数据 print(request.data) bs=BookSerializers(data=request.data) if bs.is_valid(): bs.save() # 生成记录 return Response(bs.data) else: return Response(bs.errors) class BookDetailView(APIView): def get(self,request,pk): book_obj=models.Book.objects.filter(pk=pk).first() bs=BookSerializers(book_obj,many=False) return Response(bs.data) def put(self,request,pk): book_obj = models.Book.objects.filter(pk=pk).first() bs=BookSerializers(data=request.data,instance=book_obj) if bs.is_valid(): bs.save() # update return Response(bs.data) else: return Response(bs.errors) def delete(self,request,pk): models.Book.objects.filter(pk=pk).delete() return Response("")
2、路由第二种写法-半自动方式(视图类继承ModelViewSet)
settings.py注册 'rest_framework'
serializer.py
from rest_framework.serializers import ModelSerializer from app01 import models class PublishSerializers(ModelSerializer): class Meta: model = models.Publish fields = "__all__"
models.py
from django.db import models # Create your models here. class Publish(models.Model): name=models.CharField(max_length=32) city=models.CharField(max_length=64)
makemigrations
migrate
views.py
from django.shortcuts import render from app01 import models from app01 import serializer from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet #路由的第二种写法: class PublishView(ModelViewSet): queryset=models.Publish.objects.all() serializer_class=serializer.PublishSerializers
urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})), url(r'^publish/(?P<pk>d+)$', views.PublishView.as_view({'get':'retrieve','delete':'destroy','put':'update'})), ]
测试:
3、路由第三种写法-自动生成
views.py
from django.shortcuts import render from app01 import models from app01 import serializer from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet # 必须继承ModelViewSet才可以用这种方式的路由 class PublishView(ModelViewSet): queryset=models.Publish.objects.all() serializer_class=serializer.PublishSerializers
urls.py
from django.conf.urls import url, include from django.contrib import admin from app01 import views from rest_framework.routers import SimpleRouter,DefaultRouter #SimpleRouter 自动生成两条路由;DefaultRouter自动生成四条路由 # router = SimpleRouter() router = DefaultRouter() # 注册; publish就是路径 router.register('publish', views.PublishView) urlpatterns = [ url(r'^admin/', admin.site.urls), # url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})), # url(r'^publish/(?P<pk>d+)$', views.PublishView.as_view({'get':'retrieve','delete':'destroy','put':'update'})), url(r'', include(router.urls)), ]