django restframework框架九 渲染器
@(python之路)[django restframework框架十 渲染器]
django restframework框架十 渲染器
渲染器编写
django restframework的渲染器有好多模板,这里我们只对json做说明。
settings.py
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer',]
}
这样所有的数据返回都是json格式。
如果在在单独的类中定义的话需要这样写:
from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer,AdminRenderer
class SaView(ModelViewSet):
authentication_classes = []
renderer_classes = [JSONRenderer,BrowsableAPIRenderer]
queryset = models.Sa.objects.all()
serializer_class = SaSerialiser
urls.py
urlpatterns = [
url(r'^sa/$', views.SaView.as_view({'get':'list','post':'create'})),
url(r'^sa\.(?P<format>[a-z0-9]+)$', views.SaView.as_view({'get':'list','post':'create'})),
# 详细 : SaView.get
# 修改 : SaView.put
# 局部 : SaView.patch
# 删除 : SaView.delete
url(r'^sa/(?P<pk>\d+)/$', views.SaView.as_view({'get':"retrieve",'put':'update','patch':'partial_update','delete':'destroy'})),
url(r'^sa/(?P<pk>\d+)\.(?P<format>[a-z0-9]+)$', views.SaView.as_view({'get':"retrieve",'put':'update','patch':'partial_update','delete':'destroy'})),
]