时间:2021-05-22
一、基本视图
写一个出版社的增删查改resful接口
要自己事先创建好django项目,并创建好表,添加完记录
路由:
url(r'^publish/$', views.PublishView.as_view()),
url(r'^publish/(?P<pk>\d+)/$', views.PublishDetailView.as_view()),
视图:
二、mixins类和generic类编写视图
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/(?P<pk>\d+)', views.PublishDetail.as_view()),
视图:
from api import modelsfrom rest_framework import serializersfrom rest_framework import mixinsfrom rest_framework.generics import GenericAPIViewclass PublishSer(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__'class Publish(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer def get(self,request): return self.list(request) #在ListModelMixin下有一个list方法,返回的就是查询的结果:Response(serializer.data) def post(self,request): return self.create(request) #已经封装好了,帮我们做校验和序列化,返回的是添加成功的记录class PublishDetail(GenericAPIView,mixins.DestroyModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer def get(self,request,*args, **kwargs): return self.retrieve(request,*args, **kwargs) def put(self,request,*args, **kwargs): return self.update(request,*args, **kwargs) def delete(self,request,*args, **kwargs): return self.destroy(request,*args, **kwargs)三、使用generic下ListCreateAPIView和RetrieveUpdateDestroyAPIView
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/(?P<pk>\d+)', views.PublishDetail.as_view()),
视图:
from api import modelsfrom rest_framework import mixinsfrom rest_framework.generics import GenericAPIViewfrom rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIViewfrom rest_framework import serializersclass Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__'class Publish(ListCreateAPIView): queryset = models.Publish.objects.all() serializer_class = Publishserclass PublishDetail(RetrieveUpdateDestroyAPIView): queryset = models.Publish.objects.all() serializer_class = Publishser四、使用ModelViewSet
路由:
url(r'^publish$',views.Publish.as_view({'get':'list','post':'create'})),
url(r'^publish/(?P<pk>\d+)', views.Publish.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
视图:
from rest_framework.viewsets import ModelViewSetfrom api import modelsfrom rest_framework import serializersclass Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__'class Publish(ModelViewSet): queryset = models.Publish.objects.all() serializer_class = Publishser以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这篇文章主要介绍了Djangorstful登陆认证并检查session是否过期代码实例,下面我们可以来一起学习一下。一:restful用户视图#!/usr/bi
一.前提首先是这个代码基于前后端分离的API,我们用了django的framework模块,帮助我们快速的编写restful规则的接口前端token原理:把(t
自动生成api文档(不管是函数视图还是类视图都能显示)1.安装rest_framework_swagger库pipinstalldjango-rest-swag
响应数据和结果视图返回值分类controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。//指定逻辑视图名,经过视图解析器解析为js
这篇文章主要介绍了springmvc视图解析流程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下在sp