restful api中method的使用:
增
POST /users/ # 新增一个用户
删
DELETE /users/1 # 删除一个用户
改
PUT /users/1/ # 全部修改
patch /users/1/ # 局部修改
查
get /users/ # 用户列表
/users/1/ # 单个用户
因此我们通常需要两种路由规则来匹配url中有无ID传入:
urlpatterns = [
url(r'^users/$', views.TestView.as_view()), # http://127.0.0.1:8000/users/
url(r'^users/(?P\d+)/$', views.TestView.as_view()), # http://127.0.0.1:8000/users/1/
]
django-restframework中又添加了两种规则,方便我们决定在页面上显示哪种格式的数据:
urlpatterns = [
# http://127.0.0.1:8000/api/v1/test/
url(r'^test/$', s11_render.TestView.as_view()),
# http://127.0.0.1:8000/api/v1/test.json/ 页面显示json格式
url(r'^test\.(?P[a-z0-9]+)$', s11_render.TestView.as_view()),
# http://127.0.0.1:8000/api/v1/test/1/
url(r'^test/(?P[^/.]+)/$', s11_render.TestView.as_view()),
# http://127.0.0.1:8000/api/v1/test/1.json/ 页面显示json格式
url(r'^test/(?P[^/.]+)\.(?P[a-z0-9]+)$', s11_render.TestView.as_view())
]
使用
自定义路由系统
urls.py
from django.conf.urls import url, include
from web.views import s11_render
urlpatterns = [
url(r'^test/$', s11_render.TestView.as_view()),
url(r'^test\.(?P[a-z0-9]+)$', s11_render.TestView.as_view()),
url(r'^test/(?P[^/.]+)/$', s11_render.TestView.as_view()),
url(r'^test/(?P[^/.]+)\.(?P[a-z0-9]+)$', s11_render.TestView.as_view())
]
views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from .. import models
class TestView(APIView):
def get(self, request, *args, **kwargs):
print(kwargs)
print(self.renderer_classes)
return Response('...')
半自动路由
urls.py
from django.conf.urls import url, include
from web.views import s10_generic
"""
在路由中做method与处理视图函数的映射
get
- /test/ - list 获取多个
- /test/1/ - retrieve 获取单个
post
- /test/ - create 新增
put
- /test/1/ - update 全部修改
patch
- /test/1/ - partial_update 局部修改
delete
- /test/1/ - destroy 删除
"""
urlpatterns = [
url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})),
url(r'^test/(?P\d+)/$', s10_generic.UserViewSet.as_view(
{'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
]
views.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .. import models
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserInfo
fields = "__all__"
class UserViewSet(ModelViewSet):
queryset = models.UserInfo.objects.all()
serializer_class = UserSerializer
全自动路由
urls.py
from django.conf.urls import url, include
from rest_framework import routers
from web.views import s10_generic
router = routers.DefaultRouter()
router.register(r'users', s10_generic.UserViewSet) # 自动生成匹配规则
"""
在路由中做method与处理视图函数的映射
get
- /test/ - list 获取多个
- /test/1/ - retrieve 获取单个
post
- /test/ - create 新增
put
- /test/1/ - update 全部修改
patch
- /test/1/ - partial_update 局部修改
delete
- /test/1/ - destroy 删除
"""
urlpatterns = [
url(r'^', include(router.urls)),
]
views.py
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .. import models
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserInfo
fields = "__all__"
class UserViewSet(ModelViewSet):
queryset = models.UserInfo.objects.all()
serializer_class = UserSerializer