当前位置: 首页 > news >正文

电子商务网站规划与建设百度收录最新方法

电子商务网站规划与建设,百度收录最新方法,wordpress php7 报错,web托管公司前面介绍了什么是符合RESTful规范的API接口,以及使用了基于函数的视图(FBV)编写了对文章进行增删查改的API。在本篇文章将使用基于类的视图(Class-based View, CBV)重写之前的接口。 参考: 1、Django开发总结:Django MVT与MVC设计模式&…

前面介绍了什么是符合RESTful规范的API接口,以及使用了基于函数的视图(FBV)编写了对文章进行增删查改的API。在本篇文章将使用基于类的视图(Class-based View, CBV)重写之前的接口。

参考:

1、Django开发总结:Django MVT与MVC设计模式,请求过程与代码示例(附源码)_SteveRocket的博客-CSDN博客如果要开发一个好的网站或网络应用,就必需了解经典的软件开发所遵循的MVC 设计模式。本篇详细总结软件开发所遵循的MVC (Model-View-Controller, 模型-视图-控制器) 设计模式以及Django的MVT设计模式(Model-View-Template)如何遵循这种设计理念。Django Model(模型), URL(链接), View(视图) 和Template(模板)又是如何遵循MVC软件设计模式的。https://blog.csdn.net/zhouruifu2015/article/details/129648966

https://blog.csdn.net/zhouruifu2015/article/details/129761750icon-default.png?t=N2N8https://blog.csdn.net/zhouruifu2015/article/details/129761750

工程路径及APP:django_framework\django_rest_framework_pro\drf_pro

基于类的视图(CBV)

一个中大型的Web项目代码量通常是非常大的,如果全部使用函数视图写,那么代码的复用率是非常低的。而使用类视图,就可以有效的提高代码复用,因为类是可以被继承的,可以拓展的。特别是将一些可以共用的功能抽象成Mixin类或基类后可以减少重复造轮子的工作。

DRF推荐使用基于类的视图(CBV)来开发API, 并提供了4种开发CBV开发模式。

  • 使用基础APIView类
  • 使用Mixins类和GenericAPI类混配
  • 使用通用视图generics.*类, 比如generics.ListCreateAPIView
  • 使用视图集ViewSet和ModelViewSet

类视图的比较

DRF提供了4种编写CBV类API的方式,到底哪种CBV开发模式更好? 答案是各有利弊

  • 基础的API类可读性最高,代码最多,灵活性最高。当需要对API行为进行个性化定制时,建议使用这种方式。
  • 通用generics.*类可读性好,代码适中,灵活性较高。当需要对一个模型进行标准的增删查改全部或部分操作时建议使用这种方式。
  • 使用视图集viewset: 可读性较低,代码最少,灵活性最低。当需要对一个模型进行标准的增删查改的全部操作且不需定制API行为时建议使用这种方式。
  • mixin类和GenericAPI的混用,这个和generics.*类没什么区别,不看也罢。

Django视图集viewset代码最少,但这是以牺牲了代码的可读性为代价的,因为它对代码进行了高度地抽象化。另外urls由router生成,不如自己手动配置的清楚。

使用CBV类可以简化代码,增加代码重用,在很多情况下还需要重写父类的方法,比如get_queryset, get_serializer_class方法以实现特殊的功能。

使用APIView

DRF的APIView类继承了Django自带的View类, 一样可以按请求方法调用不同的处理函数,比如get方法处理GET请求,post方法处理POST请求。不过DRF的APIView要强大得多。它不仅支持更多请求方法,而且对Django的request对象进行了封装,可以使用request.data获取用户通过POST, PUT和PATCH方法发过来的数据,而且支持插拔式地配置认证、权限和限流类

使用APIView类重写之前的函数视图

# drf_pro/views.py

# 基础APIView类
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import Http404
from .models import Article
from .serializers import ArticleSerializer2class ArticleList(APIView):"""List all articles, or create a new article."""def get(self, request, format=None):articles = Article.objects.all()serializer = ArticleSerializer2(articles, many=True)return Response(serializer.data)def post(self, request, format=None):serializer = ArticleSerializer2(data=request.data)if serializer.is_valid():# 注意:手动将request.user与author绑定serializer.save(author=request.user)return Response(serializer.data, status=status.HTTP_201_CREATED)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)class ArticleDetail(APIView):"""Retrieve, update or delete an article instance."""def get_object(self, pk):try:return Article.objects.get(pk=pk)except Article.DoesNotExist:raise Http404def get(self, request, pk, format=None):article = self.get_object(pk)serializer = ArticleSerializer2(article)return Response(serializer.data)def put(self, request, pk, format=None):article = self.get_object(pk)serializer = ArticleSerializer2(instance=article, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)def delete(self, request, pk, format=None):article = self.get_object(pk)article.delete()return Response(status=status.HTTP_204_NO_CONTENT)

或许已经注意到,这段代码跟之前基于函数的视图差别并不大。最大不同的是不需要在对用户的请求方法进行判断。该视图可以自动将不同请求转发到相应处理方法,逻辑上也更清晰。

修改url配置, 让其指向新的基于类的视图

# drf_pro/urls.py

from django.urls import re_path
from rest_framework.urlpatterns import format_suffix_patterns
from . import views, views_cbvurlpatterns = [re_path(r'^articles/$', views.article_list),re_path(r'^articles/(?P<pk>[0-9]+)$', views.article_detail),# 基于类的视图re_path(r'^articles_cbv/$', views_cbv.ArticleList.as_view()),# http://localhost/v1/articles_cbv/re_path(r'^articles_cbv/(?P<pk>[0-9]+)$', views_cbv.ArticleDetail.as_view()),
]urlpatterns = format_suffix_patterns(urlpatterns=urlpatterns)

发送GET请求到v1/articles_cbv/将看到跟上文一样的效果

发送GET请求到v1/articles_cbv/4/ 根据id查看详情

使用Mixin类和GenericAPI类混配

使用基础APIView类并没有大量简化代码,与增删改查操作相关的代码包括返回内容对所有模型几乎都是一样的。比如现在需要对文章类别Category模型也进行序列化和反序列化,只需要复制Article视图代码,将Article模型改成Category模型, 序列化类由ArticleSeralizer类改成CategorySerializer类就行了。

对于这些通用的增删改查行为,DRF已经提供了相应的Mixin类。Mixin类可与generics.GenericAPI类联用,灵活组合成所需要的视图。

使用Mixin类和generics.GenericAPI类重写的类视图

# drf_pro/views.py

# 使用Mixin类和generics.GenericAPI类重写的类视图
from rest_framework import mixins
from rest_framework import generics
class ArticleList2(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):"""GenericAPIView 类继承了APIView类,提供了基础的API视图。"""queryset = Article.objects.all()serializer_class = ArticleSerializer2def get(self, request, *args, **kwargs):return self.list(request, *args, **kwargs)def post(self, reqeust, *args, **kwargs):return self.create(reqeust, *args, **kwargs)

GenericAPIView 类继承了APIView类,提供了基础的API视图。它对用户请求进行了转发,并对Django自带的request对象进行了封装。不过它比APIView类更强大,因为它还可以通过queryset和serializer_class属性指定需要序列化与反序列化的模型或queryset及所用到的序列化器类。

这里的 ListModelMixin 和 CreateModelMixin类则分别引入了.list() 和 .create() 方法,当用户发送get请求时调用Mixin提供的list()方法,将指定queryset序列化后输出,发送post请求时调用Mixin提供的create()方法,创建新的实例对象。

DRF还提供RetrieveModelMixin, UpdateModelMixin和DestroyModelMixin类,实现了对单个对象实例的查、改和删操作,如下所示:

class ArticleDetail2(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView):queryset = Article.objects.all()serializer_class = ArticleSerializer2def 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)

已经有了get, post, delete等方法,为什么mixin类引入的方法要以list, create, retrieve, destroy方法命名?

因为请求方法不如操作名字清晰,比如get方法同时对应了获取对象列表和单个对象两种操作,使用list和retrieve方法后则很容易区分。另外post方法接受用户发过来的请求数据后,有时只需转发不需要创建模型对象实例,所以post方法不能简单等于create方法。

新的ArticleList视图类看似正确,但其实还有一个问题。 定义的序列化器ArticleSeralizer类并不包含author这个字段的,这是因为希望在创建article实例时将author与request.user进行手动绑定。在前面的例子中使用serializer.save(author=request.user)这一方法进行手动绑定。

现在使用mixin类后的操作方法是重写perform_create方法,如下所示:

class ArticleList2(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):"""GenericAPIView 类继承了APIView类,提供了基础的API视图。"""queryset = Article.objects.all()serializer_class = ArticleSerializer2def get(self, request, *args, **kwargs):return self.list(request, *args, **kwargs)def post(self, reqeust, *args, **kwargs):return self.create(reqeust, *args, **kwargs)# 将request.user与author绑定def perform_create(self, serializer):serializer.save(author=self.request.user)

perform_create这个钩子函数是CreateModelMixin类自带的,用于执行创建对象时需要执行的其它方法,比如发送邮件等功能,有点类似于Django的信号。类似的钩子函数还有UpdateModelMixin提供的.perform_update方法和DestroyModelMixin提供的.perform_destroy方法。

urls.py中新定义URL

# 使用Mixin类和generics.GenericAPI类重写的类视图
re_path(r'^articles_cbv2/$', views_cbv.ArticleList2.as_view()),
re_path(r'^articles_cbv2/(?P<pk>[0-9]+)/$', views_cbv.ArticleDetail2.as_view()),

浏览器请求文章列表

浏览器请求单篇文章详情

使用通用视图generics.*

将Mixin类和GenericAPI类混配,已经帮助减少了一些代码,但还可以做得更好,比如将get请求与mixin提供的list方法进行绑定感觉有些多余。DRF还提供了一套常用的将 Mixin 类与 GenericAPI类已经组合好了的视图,开箱即用,可以进一步简化的代码,如下所示:

from rest_framework import generics
class ArticleList3(generics.ListCreateAPIView):queryset = Article.objects.all()serializer_class = ArticleSerializer2# 将request.user与author绑定def perform_create(self, serializer):serializer.save(author=self.request.user)class ArticleDetail3(generics.RetrieveUpdateAPIView):queryset = Article.objects.all()serializer_class = ArticleSerializer2

generics.ListCreateAPIView类支持List、Create两种视图功能,分别对应GET和POST请求。generics.RetrieveUpdateDestroyAPIView支持Retrieve、Update、Destroy操作,其对应方法分别是GET、PUT和DELETE。

urls.py中新定义URL

# generic class-based views
re_path(r'articles_cbv3/$', views_cbv.ArticleList3.as_view()),
re_path(r'articles_cbv3/(?P<pk>[0-9]+)/$', views_cbv.ArticleDetail3.as_view()),


短短几行代码实现了所有想要的功能,代码更简洁,其它常用generics.*类视图还包括ListAPIView, RetrieveAPIView, RetrieveUpdateAPIView等等。

使用视图集(viewset)

使用通用视图generics.*类后视图代码已经大大简化,但是ArticleList和ArticleDetail两个类中queryset和serializer_class属性依然存在代码重复。使用视图集可以将两个类视图进一步合并,一次性提供List、Create、Retrieve、Update、Destroy这5种常见操作,这样queryset和seralizer_class属性也只需定义一次就好, 如下所示:

# drf_pro/views.py

# 视图集(viewset)
from rest_framework import viewsets
class ArticleViewSet(viewsets.ModelViewSet):# 用一个视图集替代ArticleList和ArticleDetail两个视图queryset = Article.objects.all()serializer_class = ArticleSerializer2# 自行添加,将request.user与author绑定def perform_create(self, serializer):serializer.save(author = self.request.user)

使用视图集后,需要使用DRF提供的路由router来分发urls,因为一个视图集现在对应多个urls的组合,而不像之前的一个url对应一个视图函数或一个视图类。

# drf_pro/urls.py

# 使用视图集后,需要使用DRF提供的路由router来分发urls,因为一个视图集现在对应多个urls的组合
from rest_framework.routers import DefaultRouterrouter = DefaultRouter()
router.register(r'articles4', viewset=views_cbv.ArticleViewSet)
urlpatterns = [
]
urlpatterns += router.urls

 

浏览器访问

一个视图集对应List、Create、Retrieve、Update、Destroy这5种操作。有时候只需要其中的几种操作,该如何实现?答案是在urls.py中指定方法映射即可,如下所示:

# drf_pro/urls.py

from django.urls import re_path
from rest_framework.urlpatterns import format_suffix_patterns
from . import views, views_cbv# 针对只需要其中的几种操作 使用方法映射
article_list = views_cbv.ArticleViewSet.as_view({'get': 'list','post': 'create'
})
article_detail = views_cbv.ArticleViewSet.as_view({'get': 'retrieve'  # 只处理get请求,获取单个记录
})urlpatterns = [# 视图集(viewset)re_path(r'^articles5/$', article_list),re_path(r'^articles5/(?P<pk>[0-9]+)/$', article_detail),
]

 

另外DRF还提供了ReadOnlyModelViewSet这个类,仅支持list和retrive这个操作。

代码示例:https://download.csdn.net/download/zhouruifu2015/87611605

输入才有输出,吸收才能吐纳。——码字不易


文章转载自:
http://agedly.sqLh.cn
http://apo.sqLh.cn
http://etr.sqLh.cn
http://malihini.sqLh.cn
http://paulette.sqLh.cn
http://whitsun.sqLh.cn
http://contortion.sqLh.cn
http://impious.sqLh.cn
http://gallinacean.sqLh.cn
http://felting.sqLh.cn
http://gigasecond.sqLh.cn
http://morphophonics.sqLh.cn
http://fiscality.sqLh.cn
http://bathwater.sqLh.cn
http://biflagellate.sqLh.cn
http://exciting.sqLh.cn
http://anomaloscope.sqLh.cn
http://balneary.sqLh.cn
http://ilk.sqLh.cn
http://solidi.sqLh.cn
http://cucurbitaceous.sqLh.cn
http://biramous.sqLh.cn
http://workmanlike.sqLh.cn
http://bacteriophobia.sqLh.cn
http://dockage.sqLh.cn
http://interpellate.sqLh.cn
http://split.sqLh.cn
http://fashion.sqLh.cn
http://doozy.sqLh.cn
http://cinematograph.sqLh.cn
http://bhuket.sqLh.cn
http://trabeation.sqLh.cn
http://disseisin.sqLh.cn
http://electroless.sqLh.cn
http://ionic.sqLh.cn
http://hollow.sqLh.cn
http://wainscoting.sqLh.cn
http://manservant.sqLh.cn
http://nucleoplasm.sqLh.cn
http://chauffer.sqLh.cn
http://vermicelli.sqLh.cn
http://boon.sqLh.cn
http://asiatic.sqLh.cn
http://lenticulated.sqLh.cn
http://brakesman.sqLh.cn
http://rendzina.sqLh.cn
http://cymatium.sqLh.cn
http://mnemotechnist.sqLh.cn
http://bigaroon.sqLh.cn
http://psychoanalysis.sqLh.cn
http://sally.sqLh.cn
http://hostler.sqLh.cn
http://emancipate.sqLh.cn
http://tabouret.sqLh.cn
http://protechny.sqLh.cn
http://psychoanalytic.sqLh.cn
http://rosewood.sqLh.cn
http://expansibility.sqLh.cn
http://atropin.sqLh.cn
http://babism.sqLh.cn
http://canvasback.sqLh.cn
http://pedler.sqLh.cn
http://premaxillary.sqLh.cn
http://rumford.sqLh.cn
http://blavatsky.sqLh.cn
http://efferent.sqLh.cn
http://transfluxor.sqLh.cn
http://leukocytosis.sqLh.cn
http://scrotitis.sqLh.cn
http://burst.sqLh.cn
http://nevoid.sqLh.cn
http://biotransformation.sqLh.cn
http://aromaticity.sqLh.cn
http://romanesco.sqLh.cn
http://dipody.sqLh.cn
http://webfoot.sqLh.cn
http://ticky.sqLh.cn
http://darpanet.sqLh.cn
http://phototonus.sqLh.cn
http://candent.sqLh.cn
http://pwt.sqLh.cn
http://awfulness.sqLh.cn
http://platelayer.sqLh.cn
http://render.sqLh.cn
http://gnotobiotic.sqLh.cn
http://bonism.sqLh.cn
http://guise.sqLh.cn
http://corporealize.sqLh.cn
http://grunth.sqLh.cn
http://patio.sqLh.cn
http://canoodle.sqLh.cn
http://cnidoblast.sqLh.cn
http://loof.sqLh.cn
http://resistable.sqLh.cn
http://mutchkin.sqLh.cn
http://whiting.sqLh.cn
http://chlorphenol.sqLh.cn
http://sylvan.sqLh.cn
http://duality.sqLh.cn
http://kiltie.sqLh.cn
http://www.15wanjia.com/news/87118.html

相关文章:

  • 进入公众号后打开网页莱阳seo外包
  • 做网站用的软件重庆高端seo
  • 网站文字广告代码西安疫情最新消息
  • 一个好的营销型网站模板seo是什么意思如何实现
  • 一级门户网站建设费用域名注册信息
  • 做p2p网站费用浏览广告赚钱的平台
  • 一般做网站上传的图片大小软文形式推广产品
  • 购物网站建设好处网络营销师是干什么的
  • 成品网站w灬源码伊园百度统计
  • 网站seo李守洪排名大师seo营销外包公司
  • 什么是网站建设?店铺推广软文500字
  • 商业网点建设开发中心网站龙华网站建设
  • .net 导航网站模板最新军事消息
  • 峰峰做网站b站免费版入口
  • 做健身俱乐部网站的目的和意义网络推广渠道
  • 小程序开发定制北京公司百度seo关键词外包
  • php网站开发方案最近发生的热点新闻事件
  • 做网站是靠什么赚钱搜狗搜索网
  • 九江县建设规划局网站长尾词seo排名
  • ipad做网站服务器奶茶推广软文200字
  • 深圳有哪些网站建设公司网络推广方案七步法
  • 武汉高端做网站网站搜什么关键词好
  • 珠海建设改革有哪些网站单页网站模板
  • 正规网站建设排行专业网站制作网站公司
  • 公司网站建设优点优化网站视频
  • 网站建设平台招商跟我学seo从入门到精通
  • 360做网站吗成都网站快速开发
  • 广州新塘做网站西安 做网站
  • 东莞网站建设服务商google官网浏览器
  • 建设企业学习网站百度上传自己个人简介